Pregunta

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!

¿Fue útil?

Solución

You need an extra column in dtResult to store whether that item has been checked or not. Data controls only keep information about their items between posts if that information can be stored in the data source.

Otros consejos

Referring to Renan's hint about that extra column, here is what I did:

I guess I've come up with a good solution:

After filling the DataTable, you add a bool column manually:

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

This will not affect the "RowsChanged" event and as it's an added column, before doing

adapter.Update(dtZg_Betr);

you can simply delete/remove it again (shouldn't make any difference as added columns do not affect any ChangesEvent when being deleted either)

dtZg_Betr.Columns[0].Remove(); // or Columns.Delete(...)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top