Solved

Traversing GridView

Posted on 2014-10-07
6
121 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 32

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 32

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 32

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 32

Expert Comment

by:it_saige
ID: 40368181
Not a problem.

-saige-
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now