【问题标题】:Refresh DataGridView after INSERT into SQL插入 SQL 后刷新 DataGridView
【发布时间】:2015-06-23 09:54:22
【问题描述】:

我有我的表单“InsertClient”,我有按钮点击方法

        public void Insert_Button_Click(object sender, EventArgs e)
    {
        MyClass.InsertNewClient(fullNametxt.Text, shortNametxt.Text);
        fullNametxt.Clear();
        shortNametxt.Clear();
        fullNametxt.Focus();
        GridView.Update();
        GridView.Refresh();
    }

我的班级收到这个:

    public static void InsertNewClient (String fullName, String shortName)
    {
        SqlConnection conn = DBClass.ConnectionString.GetConnection();
        SqlCommand cmd = new SqlCommand("MyStoredProcedure", conn);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@fullName", fullName);
        cmd.Parameters.AddWithValue("@shortName", shortName);
        int i = cmd.ExecuteNonQuery();
        if (i == 0)
        {
            MessageBox.Show("Can't save data");
        }
        if (i > 0)
        {
            MessageBox.Show("Data saved!");
        }
    }

问题是,数据已保存,但 DataGridView 不会在每次 INSERT(单击按钮)后刷新。我必须关闭表单并重新打开它并显示刷新。

请帮忙,我希望在插入数据后刷新 DataGridView

【问题讨论】:

  • datagridview 绑定源 =?
  • 展示你如何将数据加载到datagridview
  • @RobertA 如果您想发布解决问题的代码,您应该将其发布为答案,而不是评论。然后,如果有人阅读了这个问题并想知道您是如何解决的,他们可以直接阅读下面的内容,而不是在 cmets 中挖掘它。

标签: c# datagridview refresh


【解决方案1】:

您应该有单独的方法,例如 Refresh(),在您的情况下,它将使用 sqlDataReader 从 sql 获取数据,并且在您的 insertBtn 调用该方法并将 dataGridView DataSource 初始化为它之后

【讨论】:

    【解决方案2】:

    类似@mmking,但不完全是 我已经解决了..问题正是@Fabio所说的......

    在按钮单击方法中,我再次用 DataSet 填充了 de DataGridView。

    public void Insert_Button_Click(object sender, EventArgs e)
    {
        MyClass.InsertNewClient(fullNametxt.Text, shortNametxt.Text);
        this.tblClientTableAdapter.Fill(this.DataSetClient.tblClient);
    }
    

    只是为了清楚......我使用工具箱的DataGridView,选择“选择数据源”并选择我要用来填充DataGrid的表格......

    我使用默认提供给我的 DataSetName 并像我在代码中显示的那样正确放置

    希望对以后的问题有所帮助

    【讨论】:

      【解决方案3】:

      cCUSTOMERBindingSource是我使用ToolBox生成的BindingSource的对象。

      public void ReloadGrid()
          {
              Cursor.Current = Cursors.WaitCursor;
              cCUSTOMERBindingSource.DataSource = bd.C_CUSTOMER.ToList();
              Cursor.Current = Cursors.Default;
          }
      

      这是我调用方法的地方

      private void bunifuThinButton21_Click(object sender, EventArgs e)
          {
              C_CUSTOMER cst = new C_CUSTOMER();
              C_ACCOUNT acc = new C_ACCOUNT();
      
              cst.FIRST_NAME = txtFname.Text;
              cst.MIDDLE_NAME = txtMname.Text;
              cst.LAST_NAME = txtLname.Text;
              cst.LOCATION = txtlocation.Text;
              cst.DATE_OF_BIRTH = Convert.ToDateTime(dateTimePicker1.Text);
      
              acc.ACCOUNT_BALANCE = Convert.ToInt32(txtInitAmoun.Text);
              acc.ACCOUNT_NUMBER = Convert.ToInt32(txtAccNumber.Text);
              cst.TELEPHONE = Convert.ToInt32(txtTelephone.Text);
      
              cst.DATE_CREATE = DateTime.Now;
      
              int newID = cstDao.insertCustomer(cst.FIRST_NAME, cst.MIDDLE_NAME, cst.LAST_NAME, cst.LOCATION, cst.DATE_OF_BIRTH, cst.TELEPHONE, cst.MODIFY_BY, cst.DATE_MODIFY, cst.DATE_CREATE);
              acc.CUSTOMER_ID = newID;
              acc.DATE_CREATED = DateTime.Now;
              acc.CREATED_BY = 1;
              int newAccID  = cstDao.insertAccount(acc);
      
              if(newID != 0 && newAccID != 0) { 
              MessageBox.Show("Insert Succefull", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
      
              }
              else
              {
                  MessageBox.Show("Error during the insertion", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
              }
              ReloadGrid();
      
          }
      

      在表单加载时

      private void Form3_Load(object sender, EventArgs e)
              {
                  bd = new Customer_DBEntities();
                  cCUSTOMERBindingSource.DataSource = bd.C_CUSTOMER.ToList();
              }
      

      【讨论】:

        猜你喜欢
        • 2013-09-04
        • 1970-01-01
        • 2012-04-27
        • 2013-07-03
        • 1970-01-01
        • 1970-01-01
        • 2014-02-13
        • 2014-09-06
        • 2014-08-11
        相关资源
        最近更新 更多