andyw27
asked on
Traversing GridView
Hi,
I have a bit of a unusual requirement. I have gridview which when populated looks like this:
I need to produce some code that will look at each row.
It’ll start with 2377, it then need to work out that col 2 is blank so insert a value (which will be derived from 2377) then move on to col 3, blank so insert the same value again, same for col 4, insert value. Col 5 has a value, so do nothing, col 6 is blank so insert value (derived from col 5), col 7 is blank so insert value.
This logic needs to repeat for each row.
Can’t seem to work out how to do this so would be grateful for any suggestions?
I have a bit of a unusual requirement. I have gridview which when populated looks like this:
I need to produce some code that will look at each row.
It’ll start with 2377, it then need to work out that col 2 is blank so insert a value (which will be derived from 2377) then move on to col 3, blank so insert the same value again, same for col 4, insert value. Col 5 has a value, so do nothing, col 6 is blank so insert value (derived from col 5), col 7 is blank so insert value.
This logic needs to repeat for each row.
Can’t seem to work out how to do this so would be grateful for any suggestions?
ASKER
Thanks for the idea, I'll give it a go.
Yep, to populate row 1, col 2 I'll need to take 2377, do something which will then return a value. this returned value will go into col2, col3 and col4
Yep, to populate row 1, col 2 I'll need to take 2377, do something which will then return a value. this returned value will go into col2, col3 and col4
Ok I think I better understand your situation (had to reread your original question and your subsequent response). With that in mind, this:
-saige-
int currentCellValue = -1;
int previousCellValue = -1;
int derivedValue = -1;
for (int i = 0; i < grid.Rows.Count; i++)
{
for (int j = 0; j < grid.Rows[i].Cells.Count; j++)
{
if (int.TryParse(grid.Rows[i].Cells[j].Value.ToString(), out currentCellValue))
{
derivedValue = -1;
continue;
}
else
{
if (j == 0)
MessageBox.Show(string.Format("Row {0} - Column {1}, does not contain a valid value.", i, j));
else
{
if (int.TryParse(grid.Rows[i].Cells[j - 1].Value.ToString(), out previousCellValue))
{
if (derivedValue == -1)
derivedValue = GetDerivedValue(previousCellValue);
grid.Rows[i].Cells[j].Value = derivedValue;
}
else
MessageBox.Show(string.Format("Row {0} - Column {1}, does not contain a valid value.", i, j));
}
}
}
currentCellValue = -1;
}
Should satisfy your requirements.-saige-
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for taking the time to produce the example. Works well.
Not a problem.
-saige-
-saige-
Open in new window
Should suffice.
-saige-