数据网格视图中的复选框没有';t更新我的绑定列表
本文关键字:更新 我的 列表 绑定 视图 网格 数据网 复选框 数据 | 更新日期: 2023-09-27 18:00:02
我有一个包含2列的数据网格。一个是复选框,一个是普通的文本框单元格。所有绑定到一个BindingList,该BindingList是一个实体。
如果我选中一个复选框,然后循环从BindingList中获取选中的实体,它将不返回任何结果。但是,如果我选中然后编辑文本框列,它工作得很好,并返回一个结果。
我尝试刷新或检查,然后单击其他位置。它不起作用。
在检查列时,如何更新bindingList?
谢谢!
数据源(dataTable)中的列是什么数据类型?它是布尔型的吗?
但这并不重要,重要的是你使用了正确的dgv事件。使用:1.CurrentCellDirtyStateChanged和2.CellValueChanged
这是你必须使用的代码:
private void CreateAndBind()
{
DataTable table = GetDataToDataTable();
//then bind it to dgv:
dgv.DataSource = new BindingSource(table, null);
//create events for dgv:
dgv.CurrentCellDirtyStateChanged += new EventHandler(dgv_CurrentCellDirtyStateChanged);
dgv.CellValueChanged += new EventHandler(dgv_CellValueChanged);
}
private DataTable GetDataToDataTable()
{
//get data from dataBase, or what ever...
table.Columns.Add("column1", typeof(stirng));
table.Columns.Add("column2", typeof(bool));
//adding some exmaple rows:
table.Rows.Add("item 1", true);
table.Rows.Add("item 2", false);
return table;
}
void dgv_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void dgv_CellValueChanged(object obj, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0) //compare to checkBox column index
{
DataGridViewCheckBoxCell check = dataGridView1[0, e.RowIndex] as DataGridViewCheckBoxCell;
if (Convert.ToBoolean(check.Value) == true)
{
//If tick is added!
//
}
}
}
希望能有所帮助。