I have a DataGridView that looks like the following.
I am required to get ID value of every row and perform an operation with it in every loop. I am able to get the ID value if I select a particular row. However what I am trying to do is basically select all the rows pro-grammatically and get the ID value in every count of my for loop.
Ideally I'd get 2645 in my first iteration, 1723 in the second iteration and so on.
What I tried so far is:
if (dataGridView1.SelectedCells.Count > 0)
{
int selectedrowindex = dataGridView1.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = dataGridView1.Rows[selectedrowindex];
string IDStr = Convert.ToString(selectedRow.Cells["ID"].Value);
//an operation with the ID Value
}
This only worked for a single row and when I selected the row manually. For every row in the DataGridView, I tried to add dataGridView1.SelectAll()
and looped inside foreach(DataGridViewRow row in dataGridView1.SelectedCells)
and performed all the steps above but that did not work either.
What am I doing wrong here? Any idea/help would be greatly appreciated. I am happy to clarify if any info is unclear in the question.