Solved

Traversing GridView

Posted on 2014-10-07
6
138 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

821 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