Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi,


I am looking for a way to retain the cell selection on the DataGridView after I refresh the data set.

This is the code I am playing with is:

C#
 int cell1 = (this.agentsDataGridView.CurrentCell.RowIndex);
 int cell2 = (this.agentsDataGridView.CurrentCell.ColumnIndex);


this.agentsTableAdapter.Fill(assistReportsDataSet.Agents);
this.agentsDataGridView.CurrentCell = this.agentsDataGridView[cell1, cell2];


I get the cell location before I populate the DataGridView and then I set the location after with the two integers.

The problem is that I get the following error when I click a cell value of more than (8,8)

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.


and I can see the cell selection is constantly jumping so I obviously cannot use this method to retain cell selection.
Posted
Updated 23-Jan-11 8:13am
v2

You'll laugh when I point out the problem.

Here is the modified code (I have changed a couple of your variable names to make it clearer):
C#
int row1 = (this.agentsDataGridView.CurrentCell.RowIndex);
int col1 = (this.agentsDataGridView.CurrentCell.ColumnIndex);

this.agentsTableAdapter.Fill(assistReportsDataSet.Agents);
this.agentsDataGridView.CurrentCell = this.agentsDataGridView[col1, row1];


Did you spot the change?

The CurrentCell property expects the column first, then the row. :-D

Daft ain't it?

[Edit]
After your comment:

You might want to look at the DataGridView.SelectedCells[^] property.
You can iterate over that collection, reselecting them as you go:

C#
DataGridViewSelectedCellCollection selCells = myDGV.SelectedCells;
RefreshData();
foreach (DataGridViewCell cell in selCells)
{
  myDGV.Rows[cell.RowIndex].Cells[cell.ColumnIndex].Selected = true;
}


I have typed that in without benefit of Visual Studio, so please forgive any syntax errors, but it should give you the idea.

Good luck! :)
[/Edit]
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 23-Jan-11 18:04pm    
Well spotted! 5+
I should have noticed that when OP said anything beyond (8,8) causes the problem.
Manfred Rudolf Bihy 23-Jan-11 18:04pm    
Proposed as answer.
skatebkp 24-Jan-11 4:57am    
Thank you! Damn that is stupid, now it works as the code should.

The only problem is that it only retains the selection of one cell , if I select multiple cells it will only retain the first one selected. I think I am hacking at this solution when there is probably a whole other way to do this.

Maybe a virtual datagrid could be the solution?
skatebkp 25-Jan-11 5:09am    
Well it seems your solution has the same problem I had, I am getting the same "index out of range" error but I think with a few modifications I can get it to work.
Denis Lushchinov 5-Jun-20 6:48am    
Last example won't work unfortunately. Cause once dgv get updated, cells' indexes become (-1,0). You'll have to create a independent list/collection to save those indexes.
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
int row = e.RowIndex;
int col = e.ColumnIndex;
if (row < 0 || col != 3)
return;
if (e.FormattedValue.ToString().Equals(String.Empty))
{
}
else
{
double quantity = 0;
try
{
quantity = Convert.ToDouble(e.FormattedValue.ToString());
if (quantity == 0)
{
MessageBox.Show("The quantity can not be Zero", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
e.Cancel = true;
return;
}
}
catch
{
MessageBox.Show("The quantity should be decimal value.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
e.Cancel = true;
return;
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900