加载的时候在DataTable和DataGridView中加一层BindingSource,后续通过对BindingSource的来做来实现页面控制。
private void ReloadTaskTestRound(string viewTaskID)
{
string v_query_mode = "TASK TEST ROUND";
string v_args = "﹡" + ViewTaskID;
DataTable v_dt = Client.GetDataTableByOperation(v_query_mode + v_args);
BindingSource v_bs = new BindingSource();
v_bs.DataSource = v_dt;
v_bs.Sort = "类型,轮次";
this.dg_测试.DataSource = v_bs;
}
新增数据
private void sITToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem v_tsmi = (ToolStripMenuItem)sender;
BindingSource v_bs = (BindingSource)this.dg_测试.DataSource;
DataTable v_dt = (DataTable)v_bs.DataSource;
int v_index = JConvert.ToInt(v_dt.Compute("max(轮次)", "类型='" + v_tsmi.Text + "'").ToString()) + 1;
DataRowView v_drv = (DataRowView)v_bs.AddNew();
DataRow v_dr = v_drv.Row;
v_dr["类型"] = v_tsmi.Text;
v_dr["轮次"] = v_index.ToString();
}
删除操作,不要使用BindingSource.Positon,当定义了Sort的时候,BindingSource的Position和DataTable中的RowIndex不一致。
BindingSource的RemoveCurrent只是对DataTable中的数据打上了删除标记。
private void bt_测试_删除轮次_Click(object sender, EventArgs e)
{
if (this.dg_测试.SelectedRows.Count > 0)
{
BindingSource v_bs = (BindingSource)this.dg_测试.DataSource;
DataTable v_dt = (DataTable)v_bs.DataSource;
DataRowView v_drv = (DataRowView)v_bs.Current;
v_dt.Rows.Remove(v_drv.Row);
v_dt.AcceptChanges();
}
}