Link to home
Start Free TrialLog in
Avatar of andyw27
andyw27

asked on

Traversing GridView

Hi,

I have a bit of a unusual requirement.  I have gridview which when populated looks like this:

User generated image
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?
Avatar of it_saige
it_saige
Flag of United States of America image

Would each current columns value be derived from the previous columns value (it looks like that is what you are saying but just double checking), if so, something like:
			int currentCellValue = -1;
			int previousCellValue = -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))
						continue;
					else
					{
						if (j == 0)
							MessageBox.Show(string.Format("Column {0}, does not contain a valid value.", j));
						else 
						{
							if (int.TryParse(grid.Rows[i].Cells[j - 1].Value.ToString(), out previousCellValue))
								grid.Rows[i].Cells[j].Value = GetDerivedValue(previousCellValue);
						}
					}
				}
			}

Open in new window


Should suffice.

-saige-
Avatar of andyw27
andyw27

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
Ok I think I better understand your situation (had to reread your original question and your subsequent response).  With that in mind, this:
			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;
			}

Open in new window

Should satisfy your requirements.

-saige-
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of andyw27

ASKER

Thanks for taking the time to produce the example.  Works well.
Not a problem.

-saige-