繁体   English   中英

插入SQL后刷新DataGridView

[英]Refresh DataGridView after INSERT into SQL

我得到了表单“ 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不会刷新。 我必须关闭该窗体并重新打开它,然后看起来很刷新。

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

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();
        }

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

像@mmking这样的东西,但我还没完全解决。.问题恰好是@Fabio所说的...

在按钮单击方法内部,我再次用DataSet填充了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并将其正确设置,就像我在代码中显示的那样

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM