Issue Solved - Posted for reference.
Hi, I have a strange error occurring. I have a DataGridView that I am using to store a list of records, these records are being updated from a source by a BackgroundWorker. There is a CheckBox Column that allows the user to select records from the DataGridView for processing.
When I check the CheckBox the first row(and only the first row), the checkbox does not seem to refresh correctly and displays as unchecked. The checkbox is still being treated as checked and the logic tied to it is working correctly. This is a cosmetic issue that may confuse users. any other CheckBox in the DataGridView draws correctly at all times.
Clicking anywhere on the DataGridView causes the CheckBox state to redraw correctly.
It seems like a redrawing issue, but I am having trouble tracking down why it is drawing like this.
Some details of relevance.
It is necessary for me to reload the dataset with each refresh.
I have set the cell highlight colors to be the same as the non-highlighted ones.
I have a routine that checks the Checked state of the CheckBoxes, records the records that have been selected and rechecks the checkboxes when the dataset has been replaced. -This is working correctly
See the following code.
private void BgwDocketList_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
var checkedRows = from DataGridViewRow r in Dgv_Batch.Rows
where Convert.ToBoolean(r.Cells[0].Value) == true
select r;
List<int> L = new List<int>();
foreach (DataGridViewRow x in checkedRows)
{
L.Add((int)x.Cells[1].Value);
}
Dgv_Batch.DataSource =e.Result;
foreach (DataGridViewRow v in Dgv_Batch.Rows)
{
if (L.Count(x=>x==(int)v.Cells[1].Value)>0)
{
v.Cells[0].Value = true;
}
}
}
Have tried changing the selected cell programmatically I have tried setting focus to the DataGridView in an attempt to programmatically recreate the effect of clicking on the control. This does not redraw the checkbox. only a mouse click seems to do this.
...Ok...
While Writing this I have managed to solve the problem. I guess it helps to write down the steps to ensure they are checked out.
The issue was that after updating the checkboxes I needed to use
DataGridView.EndEdit()
I would have expected that routine to have worked on the last record last as opposed to the first. I guess this relates back to the DataGridView.DataGridViewSelectedRowCollection containing a reversed index.
I have posted this Question anyway in the hope that It may help someone with the same issue.
Aucun commentaire:
Enregistrer un commentaire