We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to loop through all cell of a DataGrid in VB.net

I found some code I liked very much in ASP.net. I'm trying to modify it so it works with VB.NET. No luck so far. There is no Items property of the grid. Please help. Any other idea will help. Thank you! Private Sub SetHierarchical(ByVal bExpanded As Boolean) Dim iCount As Int32 For iCount = 0 To dgValues.Items.Count - 1 'set the bg colour of the Customer and Order rows and the plus minus cells If dgValues.Items(iCount).Cells(1).Text <> " " Then dgValues.Items(iCount).BackColor = System.Drawing.Color.Wheat dgValues.Items(iCount).Cells(0).BackColor = System.Drawing.Color.Tan End If If dgValues.Items(iCount).Cells(3).Text <> " " Then dgValues.Items(iCount).BackColor = System.Drawing.Color.AntiqueWhite dgValues.Items(iCount).Cells(2).BackColor = System.Drawing.Color.Tan End If 'set the bg colour of the total rows If dgValues.Items(iCount).Cells(4).Text = "Customer Total" Then _ dgValues.Items(iCount).BackColor = System.Drawing.Color.AntiqueWhite If bExpanded Then 'hide + on Test all rows where there is not an expandable node If dgValues.Items(iCount).Cells(3).Text = " " Then dgValues.Items(iCount).Cells(2).Controls(0).Visible = False Else 'set the minus sign CType(dgValues.Items(iCount).Cells(2).Controls(0), LinkButton).Text = "-" CType(dgValues.Items(iCount).Cells(2).Controls(0), LinkButton).CssClass = "PlusMinus" End If 'hide + on all Collection rows where there is not an expandable node If dgValues.Items(iCount).Cells(1).Text = " " Then dgValues.Items(iCount).Cells(0).Controls(0).Visible = False Else 'set the minus sign CType(dgValues.Items(iCount).Cells(0).Controls(0), LinkButton).Text = "-" End If Else 'hide + on all Order rows where there is not an expandable node If dgValues.Items(iCount).Cells(3).Text = " " Then dgValues.Items(iCount).Cells(2).Controls(0).Visible = False Else 'set the plus sign CType(dgValues.Items(iCount).Cells(2).Controls(0), LinkButton).Text = "+" CType(dgValues.Items(iCount).Cells(2).Controls(0), LinkButton).CssClass = "PlusMinus" End If 'hide + on all Customer rows where there is not an expandable node If dgValues.Items(iCount).Cells(1).Text = " " Then dgValues.Items(iCount).Cells(0).Controls(0).Visible = False Else 'set the plus sign CType(dgValues.Items(iCount).Cells(0).Controls(0), LinkButton).Text = "+" CType(dgValues.Items(iCount).Cells(0).Controls(0), LinkButton).CssClass = "PlusMinus" End If 'hide all child nodes and rows of the root nodes If dgValues.Items(iCount).Cells(1).Text = " " Then dgValues.Items(iCount).Visible = < FONT color = #0000ff > False End If Next End Sub

7 Replies

CB Clay Burch Syncfusion Team June 25, 2002 01:32 PM

> I found some code I liked very much in ASP.net. I'm trying to modify it so it works with VB.NET. No luck so far. There is no Items property of the grid. > Please help. Any other idea will help. > Thank you! > > > > > > Private Sub SetHierarchical(ByVal bExpanded As Boolean) > Dim iCount As Int32 > For iCount = 0 To dgValues.Items.Count - 1 > 'set the bg colour of the Customer and Order rows and the plus minus cells > If dgValues.Items(iCount).Cells(1).Text <> " " Then > dgValues.Items(iCount).BackColor = System.Drawing.Color.Wheat > dgValues.Items(iCount).Cells(0).BackColor = System.Drawing.Color.Tan > End If > If dgValues.Items(iCount).Cells(3).Text <> " " Then > dgValues.Items(iCount).BackColor = System.Drawing.Color.AntiqueWhite > dgValues.Items(iCount).Cells(2).BackColor = System.Drawing.Color.Tan > End If > 'set the bg colour of the total rows > If dgValues.Items(iCount).Cells(4).Text = "Customer Total" Then _ > dgValues.Items(iCount).BackColor = System.Drawing.Color.AntiqueWhite > If bExpanded Then > 'hide + on Test all rows where there is not an expandable node > If dgValues.Items(iCount).Cells(3).Text = " " Then > dgValues.Items(iCount).Cells(2).Controls(0).Visible = False > Else > 'set the minus sign > CType(dgValues.Items(iCount).Cells(2).Controls(0), LinkButton).Text = "-" > CType(dgValues.Items(iCount).Cells(2).Controls(0), LinkButton).CssClass = "PlusMinus" > End If > 'hide + on all Collection rows where there is not an expandable node > If dgValues.Items(iCount).Cells(1).Text = " " Then > dgValues.Items(iCount).Cells(0).Controls(0).Visible = False > Else > 'set the minus sign > CType(dgValues.Items(iCount).Cells(0).Controls(0), LinkButton).Text = "-" > End If > Else > 'hide + on all Order rows where there is not an expandable node > If dgValues.Items(iCount).Cells(3).Text = " " Then > dgValues.Items(iCount).Cells(2).Controls(0).Visible = False > Else > 'set the plus sign > CType(dgValues.Items(iCount).Cells(2).Controls(0), LinkButton).Text = "+" > CType(dgValues.Items(iCount).Cells(2).Controls(0), LinkButton).CssClass = "PlusMinus" > End If > 'hide + on all Customer rows where there is not an expandable node > If dgValues.Items(iCount).Cells(1).Text = " " Then > dgValues.Items(iCount).Cells(0).Controls(0).Visible = False > Else > 'set the plus sign > CType(dgValues.Items(iCount).Cells(0).Controls(0), LinkButton).Text = "+" > CType(dgValues.Items(iCount).Cells(0).Controls(0), LinkButton).CssClass = "PlusMinus" > End If > 'hide all child nodes and rows of the root nodes > If dgValues.Items(iCount).Cells(1).Text = " " Then dgValues.Items(iCount).Visible = < FONT color = #0000ff > False > End If > Next > End Sub > > > > > > > To get at the value of a cell in w Windows Forms datagrid, you use the row and column values as indexers on the datagrid object. So, to get the value of cell 2,3, you would use code such as Dim cellValue as object = dgValues(2,3) But you cannot set BackColor and controls, etc using the type of code you have above. It is considerably more work. You can see samples of how to vary a cell color at George Shepherd's Windows Forms FAQ contains an entry entitled: How do I color a individual cell depending upon its value or some external method? Check it out at: http://www.syncfusion.com/faq/winforms/search/745.asp


PD Peter Davis June 26, 2002 05:51 AM

Clay, Thank you so very much for your answer. You showed me how to grab one cell. The problem I have is that I still don't understand how do I loop through each one cell of the current grid in VB.NET. The syntaxis shown in the example is DataGrid.Items.Count-1. But there isn't such property in vb.net ?? Thank you again.


CB Clay Burch Syncfusion Team June 26, 2002 06:05 AM

George Shepherd's Windows Forms FAQ contains an entry entitled: How can I get the number of rows in my DataGrid? Check it out at: http://www.syncfusion.com/faq/winforms/search/858.asp Another way is to access the underlying datatable and get the count from the Rows collection, myDataTable.Rows.Count.


PE Peter June 27, 2002 06:32 AM

Clay, thank you again. I'm able to count the rows and the columns in the grid without a problem. I can't make it to loop through each cell. Lets say I have (i) rows and (j) columns. How do I loop through each (i)(j) of the grig in VB.NET Peter


AD Administrator Syncfusion Team June 27, 2002 07:10 AM

> Clay, > thank you again. I'm able to count the rows and the columns in the grid without a problem. I can't make it to loop through each cell. Lets say I have (i) rows and (j) columns. How do I loop through each (i)(j) of the grig in VB.NET > Peter > dim icount as int32 dim jcount as int32 for icount = 1 to i for jcount = 1 to j myDataGrid(i,j).whatever next j next icount :-)


CB Clay Burch Syncfusion Team June 27, 2002 07:28 AM

dim icount as int32 dim jcount as int32 for icount = 1 to i for jcount = 1 to j Console.WriteLine(myDataGrid(icount,jcount).ToString()) next jcount next icount


PE Peter June 29, 2002 10:52 AM

Thank you for your help. Itried the code you posted but it doesn't work. I'm receiving an error: "Can't convert integer into System.Windows.Froms.DataGridCell" ???

Loader.
Live Chat Icon For mobile
Up arrow icon