c# DataGridView做了分页以后不能即时刷新
现在winform上有一个datagridview,我绑定了一个sql表。但是在自己写了分页以后对这张表的记录增删改以后不能立马显示,要重新生成才显示的了。我用了adapter.update,然并卵。下面贴代码。
//update函数通过一个按钮调用,可以修改datagridview记录
public void update(string aid,string aname,string atel, string add,string time_)
{
//检查变量省略
//定义主键 表名 Users 字段userid
DataColumn[] keys = new DataColumn[1];
keys[0] = ds.Tables["Users"].Columns["userid"];
//查询给定的id
ds.Tables["Users"].PrimaryKey = keys;
DataRow myrow = ds.Tables["Users"].Rows.Find(aid);
//更新记录
myrow["username"] = aname;
myrow["telphone"] = atel;
myrow["address"] = add;
myrow["createtime"] = today;
//更新表单和datasource
Adapter.Update(ds, "Users");
//Adapter.Fill(ds, "new_table");
this.show(CurrentPageNum, PageCount);//用于显示分页的函数,如下
this.dataGridView1.DataSource = ds.Tables[1];
}
//用于显示分页的函数
private void show(int start, int end)
{
DataTable dt = ds.Tables["Users"];
//这一段处理要复制的记录--可以忽略掉
int start_index = (start - 1) * PageSize;
int end_index = 0;
if (start_index + PageSize > TotalPage)
{
int remain = TotalPage - (start - 1) * PageSize;
end_index = start_index + remain - 1;
}
else
{
end_index = start_index + PageSize - 1;
}
//这里尝试复制Datatable dt的一部分记录到新的datatable new_table
//new_table定义了私有变量
for (int i = start_index; i <= end_index; ++i)
{
try
{
DataRow dr = new_table.NewRow();
dr.ItemArray = ds.Tables[0].Rows[i].ItemArray;
new_table.Rows.Add(dr);
}
catch (Exception ex)
{
}
}
//改变datagridview1的数据源 ---在这里更新了 但是new_table增改删后没有变化
Adapter.Update(ds, "new_table");
this.dataGridView1.DataSource = new_table;
}
求c#大神 感激不尽