Solved

Traversing GridView

Posted on 2014-10-07
6
142 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 34

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 34

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
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 
LVL 34

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 34

Expert Comment

by:it_saige
ID: 40368181
Not a problem.

-saige-
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

707 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