简体   繁体   English

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

[英]refresh datagridview after insert or update or delete without selecting new sql query

I just want to clarify, can i refresh datagridview after insert or update or delete without selecting new sql query again ?我只是想澄清一下,我可以在插入或更新或删除后刷新 datagridview 而不再次选择新的 sql 查询吗? i have googled it, and still have no idea to do it.我已经用谷歌搜索了它,但仍然不知道要这样做。

here's my code这是我的代码

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 is to load data, and button 5 inserting data also load data. Button 4是加载数据, button 5插入数据也加载数据。 is there a way refresh datagridview without calling employee() method again ?有没有办法在不再次调用employee()方法的情况下刷新datagridview?

You can do it in couple of ways.您可以通过几种方式做到这一点。

  1. Add the newly inserted record to your datatable (you need to use a global datatable variable for this) and refresh your Grid View using this datatable.将新插入的记录添加到您的数据表(您需要为此使用全局数据表变量)并使用此数据表刷新您的Grid View
  2. You can add the newly inserted record directly to the Grid View您可以将新插入的记录直接添加到Grid View

You can follow these techniques also for DELETE and UPDATE您也可以按照这些技术进行DELETEUPDATE

Here is the implementation of idea #1 for your existing code :这是您现有代码的想法 #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
}

Look that the datatable declaration is moved up and you need to place it in top of your class :看看数据表声明被上移了,你需要把它放在你的班级的顶部:

DataTable dtclubroom = new DataTable(); 

Nothing else need to be global.没有其他东西需要是全球性的。

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

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