Solved

Traversing GridView

Posted on 2014-10-07
6
130 Views
Last Modified: 2014-10-08
Hi,

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

gridview.png
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?
0
Comment
Question by:andyw27
  • 4
  • 2
6 Comments
 
LVL 33

Expert Comment

by:it_saige
ID: 40366851
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-
0
 

Author Comment

by:andyw27
ID: 40366903
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
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40367003
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-
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40367064
This code:
using System;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Windows.Forms;

namespace DataGridExample
{
	class Program
	{
		private static DataGridView grid = new DataGridView();
		private static DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
		private static DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
		private static DataGridViewTextBoxColumn col3 = new DataGridViewTextBoxColumn();
		private static DataGridViewTextBoxColumn col4 = new DataGridViewTextBoxColumn();
		private static DataGridViewTextBoxColumn col5 = new DataGridViewTextBoxColumn();
		private static DataGridViewTextBoxColumn col6 = new DataGridViewTextBoxColumn();
		private static DataGridViewTextBoxColumn col7 = new DataGridViewTextBoxColumn();

		static void Main(string[] args)
		{
			grid.Columns.Add(col1);
			grid.Columns.Add(col2);
			grid.Columns.Add(col3);
			grid.Columns.Add(col4);
			grid.Columns.Add(col5);
			grid.Columns.Add(col6);
			grid.Columns.Add(col7);

			grid.Rows.Clear();
			grid.Rows.Add(new object[] { "2377", "", "", "", "5127", "", "" });
			grid.Rows.Add(new object[] { "4609", "", "5214", "", "", "", "" });
			grid.Rows.Add(new object[] { "3656", "", "2403", "", "2718", "", "" });
			grid.Rows.Add(new object[] { "4405", "", "", "", "4635", "", "" });
			grid.Rows.Add(new object[] { "6824", "", "3393", "", "2556", "", "" });
			grid.Rows.Add(new object[] { "1935", "", "", "", "", "", "" });

			Console.WriteLine("Before the derived values");
			Console.WriteLine("COL1\tCOL2\tCOL3\tCOL4\tCOL5\tCOL6\tCOL7");
			for (int i = 0; i < grid.Rows.Count; i++)
				Console.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}", grid.Rows[i].Cells[0].Value, grid.Rows[i].Cells[1].Value, grid.Rows[i].Cells[2].Value, grid.Rows[i].Cells[3].Value, grid.Rows[i].Cells[4].Value, grid.Rows[i].Cells[5].Value, grid.Rows[i].Cells[6].Value));

			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 (grid.Rows[i].Cells[j].Value != null)
					{
						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;
			}

			Console.WriteLine("After the derived values");
			Console.WriteLine("COL1\tCOL2\tCOL3\tCOL4\tCOL5\tCOL6\tCOL7");
			for (int i = 0; i < grid.Rows.Count; i++)
				Console.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}", grid.Rows[i].Cells[0].Value, grid.Rows[i].Cells[1].Value, grid.Rows[i].Cells[2].Value, grid.Rows[i].Cells[3].Value, grid.Rows[i].Cells[4].Value, grid.Rows[i].Cells[5].Value, grid.Rows[i].Cells[6].Value));
			Console.ReadLine();
		}

		private static int GetDerivedValue(int value)
		{
			return value * 2;
		}
	}
}

Open in new window

Produces the following output:Capture.JPG
-saige-
0
 

Author Comment

by:andyw27
ID: 40368168
Thanks for taking the time to produce the example.  Works well.
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40368181
Not a problem.

-saige-
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now