【问题标题】:refresh datagridview after insert or update or delete without selecting new sql query插入或更新或删除后刷新datagridview而不选择新的sql查询
【发布时间】:2017-10-18 04:40:06
【问题描述】:

我只是想澄清一下,我可以在插入或更新或删除后刷新 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插入数据也是加载数据。有没有办法在不再次调用employee() 方法的情况下刷新datagridview?

【问题讨论】:

    标签: c# sql winforms


    【解决方案1】:

    您可以通过多种方式做到这一点。

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

    DELETEUPDATE 也可以使用这些技术

    这是您现有代码的想法 #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(); 
    

    没有其他东西需要是全局的。

    【讨论】:

    • 如何?,仍然无法工作,直到我将数据表声明为全局并将 sqladapter 声明为全局。但数据表是重复的。无论如何除了声明为全局之外,因为以某种形式还有很多要插入的数据也显示
    • @chopperfield - 我更新了我的答案,请试试这个。您不需要拥有全局sqladapter。顺便说一句,您不需要为如此多的数据编写很多代码,只需在适当的地方以通用的方式编写一次,然后在任何地方都可以使用它。
    • 我试过了,不行。在逻辑上,数据表仍然使用选择一个(旧)。所以当然它不会在插入后更新:|
    • 对不起,我忘了添加一个块!很快就会更新答案。
    • 好吧,它有效,但有点可疑。因为手动将一行添加到 datagridview 中。因为假设它在插入过程中失败了,但是数据网格仍然添加了一个新行 :v 。好吧,这只是放置添加新行代码的流程。不过谢谢
    猜你喜欢
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多