How can I build a String by reading through a Data Grid?
-
Hello all, I need a little help on how to do something. I am working with VB.Net 2005 and I need to read through each row on a Data Grid and grab the values of each field and save it to a formatted String so I can pass that String over to a Crystal Report as a parameter. I can handle passing the String to CR but building it from the Data Grid is where I am getting stuck. I need for it look like this…. Row1Value1(tab) Row1Value2(tab) Row1Value3 Row2Value1(tab) Row2Value2(tab) Row2Value3 I really appreciate any help/suggestions that anyone can provide.
-
Hello all, I need a little help on how to do something. I am working with VB.Net 2005 and I need to read through each row on a Data Grid and grab the values of each field and save it to a formatted String so I can pass that String over to a Crystal Report as a parameter. I can handle passing the String to CR but building it from the Data Grid is where I am getting stuck. I need for it look like this…. Row1Value1(tab) Row1Value2(tab) Row1Value3 Row2Value1(tab) Row2Value2(tab) Row2Value3 I really appreciate any help/suggestions that anyone can provide.
I can see this getting ugly very fast... All you have to do is iterate over the collection of rows in the DataGridView. And in each Row, iterate over the collection of cells:
For Each r As DataGridViewRow in DataGridView1.Rows
For Each c As DataGridViewCell in r
Dim value As String = c.Value.ToString()
Next
NextYou'll, of course, have to modify this for your own situation. It won't concatenate a string together. It just shows you have to get at the values.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
I can see this getting ugly very fast... All you have to do is iterate over the collection of rows in the DataGridView. And in each Row, iterate over the collection of cells:
For Each r As DataGridViewRow in DataGridView1.Rows
For Each c As DataGridViewCell in r
Dim value As String = c.Value.ToString()
Next
NextYou'll, of course, have to modify this for your own situation. It won't concatenate a string together. It just shows you have to get at the values.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Hello all, I need a little help on how to do something. I am working with VB.Net 2005 and I need to read through each row on a Data Grid and grab the values of each field and save it to a formatted String so I can pass that String over to a Crystal Report as a parameter. I can handle passing the String to CR but building it from the Data Grid is where I am getting stuck. I need for it look like this…. Row1Value1(tab) Row1Value2(tab) Row1Value3 Row2Value1(tab) Row2Value2(tab) Row2Value3 I really appreciate any help/suggestions that anyone can provide.
Hi, To set a String with tabs, try the following code. Its a modified version of what Dave provided.
Dim mainStr as String = "" Dim rowStr as String = "" For Each r As DataGridViewRow in DataGridView1.Rows rowStr = "" For Each c As DataGridViewCell in r If (c.ColumnIndex > 0) Then rowStr += ControlChars.Tab End If rowStr += c.Value.ToString() Next mainStr += rowStr + ControlChars.CrLf Next
This code will produce the results as u wanted i.e.CCG3 wrote:
Row1Value1(tab) Row1Value2(tab) Row1Value3 Row2Value1(tab) Row2Value2(tab) Row2Value3
Hope this helps to your expectations.
Thanks Terry