Last post Jan 10, 2019 01:27 AM by TW88
Jan 07, 2019 08:45 AM|TW88|LINK
When the gridview datasource is set at HTML source page, i can check the column.count to do some modification at Gridview.databound
However, if the datasource is dynamic, i have to set the datasource at page.load, i cannot check the column.count, even at the end of page.load
Grateful for any advice. Thanks a lot.
Private Sub GridView1_DataBound(sender As Object, e As EventArgs) Handles GridView1.DataBound
If GridView1.Rows.Count > 0 Then
For i = 0 To GridView1.Columns.Count - 1
Jan 07, 2019 01:38 PM|jzero|LINK
Your best choice is Count for Row.Cells instead of Columns.
GridView.Columns consider only columns defined on GridView Control
if GridView1.Rows.Count > 0 then
dim myRow as GridViewRow = GridView1.Rows(0)
dim myCells as TableCellCollection = myRow.Cells
dim QtyCells as integer = myCells.Count for i = 0 to QtyCells - 1 ' Do here what you need, with Cell next
When there is no columns defined on GridView control. GridView.Columns is always "0"
<asp:GridView ID="GridView1" runat="server">
But when you set columns, Gridview.Columns it will return number of columns defined, in this case will return 2
<asp:GridView ID="GridView1" runat="server"> <Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Account" HeaderText="Account" />
Jan 08, 2019 02:26 AM|TW88|LINK
Thanks a lot. it works
But i found another problem
My purpose is to loop each cells in each columns, if no text in the cells of the column, then hide the column
it works if the columns are defined in the Gridview. How about if no, they are created at runtime code.
Grateful for any ideas? Thanks.
Jan 08, 2019 01:48 PM|jzero|LINK
Move to GridView.RowDataBound and leave GridView. Databound
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim QtyCells = e.Row.Cells.Count
For i = 0 To QtyCells - 1
If e.Row.Cells(i).Text = "XYZ" Then
e.Row.Cells(i).Visible = False
GridView1.HeaderRow.Cells(i).Visible = False
Jan 10, 2019 01:27 AM|TW88|LINK
Finally i put it in Gridview PreRender event. Thanks a lot for your help