C# 在插入、更新或删除后刷新datagridview,而不选择新的sql查询
我只是想澄清一下,我可以在插入、更新或删除后刷新datagridview,而不必再次选择新的sql查询吗? 我已经在谷歌上搜索过了,但仍然不知道该怎么做 这是我的密码C# 在插入、更新或删除后刷新datagridview,而不选择新的sql查询,c#,sql,winforms,C#,Sql,Winforms,我只是想澄清一下,我可以在插入、更新或删除后刷新datagridview,而不必再次选择新的sql查询吗? 我已经在谷歌上搜索过了,但仍然不知道该怎么做 这是我的密码 private void button4_Click(object sender, EventArgs e) { employee(); } public void employee() { DataTable dtclubroom = new DataTable(
private void button4_Click(object sender, EventArgs e)
{
employee();
}
public void employee()
{
DataTable dtclubroom = new DataTable();
SqlCommand command = new SqlCommand();
SqlDataAdapter adapter = new SqlDataAdapter(command.CommandText, myConnection);
try
{
myConnection.Open();
dtclubroom.Clear();
command.Connection = myConnection;
command.CommandText = "Select * from employee ";
adapter.SelectCommand = command;
adapter.Fill(dtclubroom);
dataGridView2.DataSource = dtclubroom;
}
catch (Exception ex)
{
MessageBox.Show("error" + ex);
}
finally
{
myConnection.Close();
}
}
private void button5_Click(object sender, EventArgs e)
{
SqlCommand command2 = new SqlCommand();
try
{
myConnection.Open();
command2.CommandText = "insert into employee (name,id) values (@name,@id)";
command2.Connection = myConnection;
command2.Parameters.AddWithValue("@name","Leon");
command2.Parameters.AddWithValue("@id", "002");
command2.ExecuteNonQuery();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
finally
{
myConnection.Close();
}
employee() //<- refresh datagridview
}
按钮4加载数据,按钮5插入数据也加载数据。有没有一种方法可以在不再次调用employee方法的情况下刷新datagridview?您可以通过两种方法来完成 将新插入的记录添加到datatable您需要为此使用全局datatable变量,并使用此datatable刷新网格视图。 可以将新插入的记录直接添加到网格视图中 您也可以遵循这些技术进行删除和更新 以下是idea 1在现有代码中的实现:
DataTable dtclubroom = new DataTable();
private void button4_Click(object sender, EventArgs e)
{
employee();
}
public void employee()
{
SqlCommand command = new SqlCommand();
SqlDataAdapter adapter = new SqlDataAdapter(command.CommandText, myConnection);
try
{
myConnection.Open();
dtclubroom.Clear();
command.Connection = myConnection;
command.CommandText = "Select * from employee ";
adapter.SelectCommand = command;
adapter.Fill(dtclubroom);
dataGridView2.DataSource = dtclubroom;
}
catch (Exception ex)
{
MessageBox.Show("error" + ex);
}
finally
{
myConnection.Close();
}
}
private void button5_Click(object sender, EventArgs e)
{
SqlCommand command2 = new SqlCommand();
try
{
myConnection.Open();
command2.CommandText = "insert into employee (name,id) values (@name,@id)";
command2.Connection = myConnection;
command2.Parameters.AddWithValue("@name","Leon");
command2.Parameters.AddWithValue("@id", "002");
command2.ExecuteNonQuery();
DataRow dr = dtclubroom.NewRow();
dr["name"] = "Leon";
dr["id"] = "002";
dtclubroom.Rows.Add(dr);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
finally
{
myConnection.Close();
}
dataGridView2.DataSource = dtclubroom; //<- refresh datagridview
}
其他任何东西都不需要是全局的。how?仍然不起作用,除非我将datatable声明为全局,并将sqladapter声明为全局。但是数据表是重复的。除了声明为全局之外,还有其他原因吗?因为在某种形式下,需要插入的数据太多了displayed@chopperfield-我更新了我的答案,请试试这个。您不需要有全局sqladapter。顺便说一句,你不需要为这么多的数据编写太多的代码,只需在适当的地方以通用的方式编写一次,然后在任何地方使用它。我已经尝试过了,但没有成功。在逻辑上,数据表仍然使用select one old。因此,插入后它当然不会更新:|对不起,我忘记添加块了!很快更新答案。好吧,它有效,但有点可疑。因为手动将行添加到datagridview中。因为假设它在insert期间失败了,但是datagrid仍然添加了一个新行:v。嗯,这只是一个流程,将添加新行代码放在哪里。谢谢
DataTable dtclubroom = new DataTable();