C# Updating/refresh 网格视图
C# Updating/refresh gridview
当我尝试更新我的 gridview 时。更改不会显示。但是当我重新打开表格时,它已经改变了。我怎样才能确保它会立即更新(并显示)。当我说:
scb = new SqlCommandBuilder(da);
da.Fill(dt);
userInformation.DataSource = dt;
显然会填满 table。但是比我有两次信息。
这是我目前的代码:
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;
我也试过刷新,但这也不起作用。
每次 Fill
DataTable
的实例时,您都需要在 DataTable
上调用 Clear()
方法。如果没有,Fill
方法只会继续添加到 DataTable
.
当我尝试更新我的 gridview 时。更改不会显示。但是当我重新打开表格时,它已经改变了。我怎样才能确保它会立即更新(并显示)。当我说:
scb = new SqlCommandBuilder(da);
da.Fill(dt);
userInformation.DataSource = dt;
显然会填满 table。但是比我有两次信息。
这是我目前的代码:
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;
我也试过刷新,但这也不起作用。
每次 Fill
DataTable
的实例时,您都需要在 DataTable
上调用 Clear()
方法。如果没有,Fill
方法只会继续添加到 DataTable
.