1

I use a DataGridView which is data-bound over a binding source. Additionally I create one CheckBoxCol on the very left for the user. Checking boxes in there and then re-sorting makes all checks disappear. Has anyone an idea how to avoid that?

Here's some code so we're on the same page ;):

    dtZg_Betr = new DataTable(); // DataTable object
    // [...] SQL SELECT and so on, I cut that stuff a bit
    adapter.Fill(dtZg_Betr); // OleDbAdapter -> fill the table with the SQL SELECT results
    // [...]
    bsZg_Betrn = new BindingSource(); // BindingSource object
    bsZg_Betrn.DataSource = dtZg_Betr;
    dgvZg_Betr.DataSource = bsZg_Betr; // bind data to DataGridView
    DataGridViewCheckBoxColumn dgvCheckBox = new DataGridViewCheckBoxColumn();
    dgvZg_Betr.Columns.Insert(0, (DataGridViewColumn)dgvCheckBox); // add additional checkbox column


    // Later on somewhere else:
    adapter.Update(dtZg_Betr); // OleDbAdapter -> update DB with table's changes

The DataGridView has no further code as such to get filled, later on of course checkbox clicks and such are handled but that should not be relevant for the issue.

Kinds regards!

4

2 回答 2

1

参考 Renan 关于那个额外专栏的提示,这就是我所做的:

我想我想出了一个很好的解决方案:

填充 DataTable 后,手动添加一个 bool 列:

// [...]
adapter.Fill(dtZg_Betr);
bsZg_Betrn = new BindingSource(); // BindingSource object
bsZg_Betrn.DataSource = dtZg_Betr;
dgvZg_Betr.DataSource = bsZg_Betr; // bind data to DataGridView

这不会影响“RowsChanged”事件,因为它是一个添加的列,在做之前

adapter.Update(dtZg_Betr);

您可以简单地再次删除/删除它(不应该有任何区别,因为添加的列在被删除时也不会影响任何 ChangesEvent)

dtZg_Betr.Columns[0].Remove(); // or Columns.Delete(...)
于 2013-05-18T18:53:10.953 回答
1

您需要一个额外的列dtResult来存储该项目是否已被选中。如果该信息可以存储在数据源中,则数据控件仅在帖子之间保留有关其项目的信息。

于 2013-05-15T12:37:05.340 回答