locked
Check Gridview number of columns RRS feed

  • Question

  • User363578564 posted

    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

    check code.....

    Next
    End If
    End Sub

    Monday, January 7, 2019 8:45 AM

Answers

  • User-943250815 posted

    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 end if

    When there is no columns defined on GridView control. GridView.Columns is always "0"

    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>

    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" /> </Columns>
    </asp:GridView>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 7, 2019 1:38 PM

All replies

  • User-943250815 posted

    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 end if

    When there is no columns defined on GridView control. GridView.Columns is always "0"

    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>

    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" /> </Columns>
    </asp:GridView>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 7, 2019 1:38 PM
  • User363578564 posted

    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.

    Tuesday, January 8, 2019 2:26 AM
  • User-943250815 posted

    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
            End If
          Next
        End If
      End Sub

    Tuesday, January 8, 2019 1:48 PM
  • User363578564 posted

    Finally i put it in Gridview PreRender event.  Thanks a lot for your help

    Thursday, January 10, 2019 1:27 AM