Reputation: 175
I have the following code:
int countRows = dataGridView3.SelectedRows.Count;
int rowIndex = 0;
foreach (DataGridViewRow row in dataGridView3.SelectedRows)
{
int selectedRowIndex = dataGridView3.SelectedCells[rowIndex].RowIndex;
DataGridViewRow selectedRow = dataGridView3.Rows[selectedRowIndex];
capacity = Convert.ToInt32(selectedRow.Cells["Cust_Number"].Value);
capStore.Add(capacity);
rowIndex++;
}
I try to go through each selected row in my DataGridView and store the value from the column "Cust_Number" into an ArrayList, so that I can change it later. Somehow he just grabs the second row each time I iterate and I have the same value in my ArrayList duplicated. What is wrong here?
Upvotes: 0
Views: 1438
Reputation: 2924
I would try the following code :
if(dataGridView3.SelectedRows != null && dataGridView3.SelectedRows.Count > 0)
{
foreach (DataGridViewRow dgvr in dataGridView3.SelectedRows)
{
int tempVal = 0;
if(dgvr.Cells["Cust_Number"].Value != null && int.TryParse(dgvr.Cells["Cust_Number"].Value.ToString(), out tempVal))
{
capStore.Add(tempVal);
}
}
}
This is simpler and safer.
Upvotes: 1