Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Visual-Studio DataTable , +
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:
 
       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 23-Jan-11 8:35am
Edited 23-Jan-11 9:13am
Henry Minute223.8K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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):
  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. Big Grin | :-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:
 
  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! Smile | :)
[/Edit]
  Permalink  
v2
Comments
Manfred R. Bihy at 23-Jan-11 18:04pm
   
Well spotted! 5+
I should have noticed that when OP said anything beyond (8,8) causes the problem.
Manfred R. Bihy at 23-Jan-11 18:04pm
   
Proposed as answer.
skatebkp at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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;
}
}
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 381
1 Sergey Alexandrovich Kryukov 245
2 Marcin Kozub 225
3 Praneet Nadkar 217
4 /\jmot 189
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 5 Sep 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100