插入或更新或删除后刷新datagridview而不选择新的SQL查询

时间:2017-10-18 04:40:07

标签: c# sql winforms

我只是想澄清一下,我可以在插入或更新或删除后刷新datagridview而不再选择新的SQL查询吗? 我用谷歌搜索了它,但仍然不知道这样做。

这是我的代码

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
    }

Button 4是加载数据,button 5插入数据也是加载数据。有没有办法刷新datagridview而不再调用employee()方法?

1 个答案:

答案 0 :(得分:1)

你可以通过几种方式实现。

  1. 将新插入的记录添加到数据表中(您需要使用全局数据表变量)并使用此数据表刷新Grid View
  2. 您可以将新插入的记录直接添加到Grid View
  3. 您还可以针对 DELETE 更新

    来遵循这些技巧

    以下是现有代码的想法#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
    }
    

    看看数据表声明已向上移动,您需要将其放在班级的顶部:

    DataTable dtclubroom = new DataTable(); 
    

    没有其他东西需要全球化。