C# 插入/删除数据后,datagridview不显示插入的项
C# 插入/删除数据后,datagridview不显示插入的项,c#,winforms,C#,Winforms,DataGridView.Refresh()是图形刷新,而不是数据刷新。 将DatagridView重新绑定到源 private void btndelete_Click(object sender, EventArgs e) { if (txtserch.Text == "") { MessageBox.Show("Enter Customer name to delete"); } else { l1.conn();
DataGridView.Refresh()
是图形刷新,而不是数据刷新。
将DatagridView重新绑定到源
private void btndelete_Click(object sender, EventArgs e)
{
if (txtserch.Text == "")
{
MessageBox.Show("Enter Customer name to delete");
}
else
{
l1.conn();
SqlCommand cmd = new SqlCommand("delete from CUSTOMER where NAME='" + txtserch.Text + "'", l1.connection);
int a = cmd.ExecuteNonQuery();
if (a == 1)
{
MessageBox.Show("Deleted Sucessfully");
dataGridView1.Refresh();
dataGridView1.RefreshEdit();
dataGridView1.Update();
txtserch.Text = "";
}
else
{
MessageBox.Show("Customer Not Exist");
txtserch.Text = "";
}
}
}
为了刷新网格,请重新绑定它
DataGridView dataGridView1 = new DataGridView();
dataGridView1.DataSource = source;
// Update Data in src1
dataGridView1.DataSource = null;
dataGridView1.DataSource = source;
这里ds是您提供给网格的数据源
有关详细信息,请参阅此如果要在编辑/删除/更新操作后将数据绑定到Gridview,则需要在下面的行代码中写入
MyGridView.DataSource = ds;
MyGridView.DataBind();
亲爱的,你必须重新绑定网格
GridviewName.DataSource = ds;// retrieve the details and store in ds
GridViewName.DataBind();