简体   繁体   English

C#更新/刷新GridView

[英]C# Updating/refresh gridview

When I'm trying to update my gridview. 当我尝试更新我的gridview时。 The changes won't show up. 更改不会显示。 But when I reopen the form it has changed. 但是,当我重新打开表格时,它已更改。 How can I make sure that it will update immediately (and show up). 我如何确保它会立即更新(并显示)。 When I say: 当我说:

            scb = new SqlCommandBuilder(da);
            da.Fill(dt);
            userInformation.DataSource = dt;

It wil obviously fill the table. 它显然会填满桌子。 But than I have the information twice. 但是比我有两次信息。

This is my code currently: 这是我目前的代码:

            DataTable dt;
            SqlDataAdapter da;
            SqlCommand cmd;
            SqlCommandBuilder scb;

            //this is inside the constructor
            cmd = new SqlCommand("loadUserTable", conn);
            cmd.Parameters.AddWithValue("@username", this.username);
            cmd.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(cmd);
            da.SelectCommand = cmd;
            dt = new DataTable();
            da.Fill(dt);
            userInformation.DataSource = dt;

            //this is a button (called save)
            scb = new SqlCommandBuilder(da);
            da.Update(dt);
            userInformation.DataSource = dt;

Also I've tried refresh but that also doesn't do the job. 我也尝试过刷新,但这也没有做。

Each time you Fill an instance of DataTable , before you do so, you need to call the Clear() method on that DataTable . 每次Fill DataTable的实例之前,都需要在该DataTable上调用Clear()方法。 If not, the Fill method just keeps adding on to that DataTable . 如果不是,则Fill方法只是继续添加到该DataTable

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

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