1
votes

We have a datagridview. Its multiselect property is set to true. This datagridview will update its rows in a particular interval time say one or two seconds.

So suppose i will select a row and then hold shift key and select another row, then the whole rows between these two is selected without any issue.

Suppose if i select a row and after that the gridview is updated, and then if i am trying to select another row by holding the shift key , then its not selecting the whole rows, instead the last clicked row is selected.

So i am not sure why its happening, so its like if i am selecting the 2nd row of the grid and updation may be this row is moved down say 5th row, and after that i am trying to select another row holding shift key, then this error is happening.

Anyone please give your suggestions on this issue.

code for selection after updation

for (int nRow = 0; nRow < Grid.Rows.Count; nRow++)
{
  foreach (string cookieIdval in SelectedCookies)
  {
     if (Grid.Rows[nRow].Cells[ColCookieID].Value.ToString() == cookieIdval.ToString())
     {
       Grid.Rows[nRow].Cells[ColCookieID].Selected = true;
       break;
     }
  }
}
3
How you update datagridview?Hamlet Hakobyan
I'm confused with your question. Are you expecting it not to select all the rows in between when you Shift select? And if I understood you wrong, and if you want to select many rows while they are updating, it doesn't sound like a good thing to me.hattenn
@hattenn : sorry for the confusion. What i want is the multiselect should work in all cases whether or not the gridview updated. ie it should select multiple rows in all cases.. hope you clear this..Mahesh KP
But what is your expected behavior? If you choose the rows between the first and the tenth, and if the second row moves to 50th place? Do you still want it selected? So when you select rows, which state will be used to pick the data rows? Before updation, or after?hattenn
@HamletHakobyan : its using a subsrciption technique. so whenever there is a change in some properties or the columns in the gridview it will update the gridview.Mahesh KP

3 Answers

1
votes

Can't you postpone update until user interraction like this ends?

0
votes

Modify your update method such that it resotres what have been selected before update. Use DataGridViewRow.Selected and DataGridView.CurrentRow properties.

The way you do this depends on how your DataGridView is being populated.

EDIT:

It seems that the key to distinguish your rows are in ColCookieID column. We could use that to store and recover selected rows:

public void UpdateGridView()
{
    HashSet<string> selectedRows = new HashSet<string>();
    foreach (DataGridViewRow row in grid.Rows)
    {
        if (row.Selected)
            selectedRows.Add(row.Cells[ColCookieID].Value.ToString());
    }
    var currentRow = grid.CurrentRow.Cells[ColCookieID].Value.ToString()

    // ... update the grid

    foreach (DataGridViewRow row in grid.Rows)
    {
        var id = row.Cells[ColCookieID].Value.ToString();
        if (selectedRows.Contains(id))
            row.Selected = true;
        if (currentRow == id)
            grid.CurrentCell = row.Cells[0];
    }
}

Let me know if it works.

0
votes

In order to address the actual problem (shift key does not work for multiselect after refresh) - this is fixed by setting

dataGridView.CurrentCell = ...

(setting CurrentCell is contained in the accepted answer, but this specific issue has not been mentioned)