I have a pretty simple general programming problem, yet I can’t quite get the results that I am looking for. Basically, I have a DataGridView called dataGridView3 (2 columns), and I want the user to be able to enter as many fields as he/she desires and create a series of strings based on these fields. My problem is that the formed string can only contain 4 elements of the dataGridView at a time, but with my current implementation, it puts every line in first string (see example).
Here is the code that I am currently using.
for (int i = 0; i < dataGridView3.Rows.Count; i++)
{
if (i % 4 == 0)
{
newPA61String += "PA61";
for (int j = i; j < dataGridView3.Rows.Count - 1; j++)
{
newPA61String += " " + (dataGridView3.Rows[j].Cells[0].Value.ToString().PadLeft(5, leftPaddingChar))
+ " " + (dataGridView3.Rows[j].Cells[1].Value.ToString().PadRight(6, rightPaddingChar));
}
newPA61String = newPA61String.PadRight(56, rightPaddingChar);
newPA61String += "\r\n";
}
}
Using 6 rows of data, this code gives me an output of:
PA61 00001 a 00002 b 00003 c 00004 d 00005 e 00006 f
PA61 00005 e 00006 f
I would like it to look like this; however, I can’t quite manipulate it the way I want:
PA61 00001 a 00002 b 00003 c 00004 d
PA61 00005 e 00006 f
My first instinct was just to truncate the first string after the specified length was reached, but the string is formed by placing everything on one line split by a “\r\n”, so doing that would eliminate anything after the first 4 columns.
I know the solution is probably fairly obvious, but I’m having one of those days…
Thanks in advance, and if you need any additional information, please ask.
Change:
to:
And you can move the Math.Min expression out of the loop.
Though I find while loops clearer for this kind of thing.