Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Traversing GridView

Posted on 2014-10-07
6
Medium Priority
?
145 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 35

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 35

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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 35

Accepted Solution

by:
it_saige earned 2000 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 35

Expert Comment

by:it_saige
ID: 40368181
Not a problem.

-saige-
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

581 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