How to hide a column in datagrid with AutoGenerateCol=true

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thanh Nu

    How to hide a column in datagrid with AutoGenerateCol=true

    Hi,

    I would like to hide a column in a web datagrid (with
    create columns automatically at runtime checked), and I
    cannot refer to the columns collection like this:
    DataGrid1.Colum ns(0).Visible = False
    (the message at runtime is something like index out of
    range, and under the debuger, I discover that the
    attribute count of the columns collection is 0!)
    Below is my piece of code.
    Could someone help me to solve this problem without
    having to bind the datagrid manually, because I have a
    sort of generic application, in which I cannot know in
    advance the column name.
    (Remark : in the catch part, I can manage to hind the
    data of the column, but I cannot hide the header, and all
    my data columns are shifted left)
    Thanks in advance,

    ------------- Code ------------------
    Private Sub Page_Load(ByVal sender As System.Object,
    ByVal e As System.EventArg s) Handles MyBase.Load
    Dim msg As String
    'Put user code to initialize the page here
    SqlConnection1. Open()
    Dim rq As String = "select * from Categories"
    Dim cmd As New SqlCommand(rq, SqlConnection1)
    Dim dr As SqlDataReader = cmd.ExecuteRead er
    Dim objItem As DataGridItem
    Dim ThisBackColor As New System.Drawing. Color
    DataGrid1.DataS ource = dr
    DataGrid1.DataB ind()
    Try
    DataGrid1.Colum ns(0).Visible = False
    Catch ex As Exception
    For Each objItem In DataGrid1.Items
    objItem.Cells(0 ).Visible = False
    Next
    End Try
    End Sub
  • GSK

    #2
    How to hide a column in datagrid with AutoGenerateCol =true

    declare a variable in your page.
    int HideColumn;

    Turn the visibility of required column to Off on
    ItemBound

    public void OnItemBound(Obj ect Sender,
    DataGridItemEve ntArgs e)
    {
    // Get the newly created item
    int i;
    ListItemType itemType = e.Item.ItemType ;

    if(HideColumn >=0 && HideColumn<e.It em.Controls.Cou nt)
    e.Item.Cells[HideColumn].Visible = false;
    }
    [color=blue]
    >-----Original Message-----
    >Hi,
    >
    >I would like to hide a column in a web datagrid (with
    >create columns automatically at runtime checked), and I
    >cannot refer to the columns collection like this:
    >DataGrid1.Colu mns(0).Visible = False
    >(the message at runtime is something like index out of
    >range, and under the debuger, I discover that the
    >attribute count of the columns collection is 0!)
    >Below is my piece of code.
    >Could someone help me to solve this problem without
    >having to bind the datagrid manually, because I have a
    >sort of generic application, in which I cannot know in
    >advance the column name.
    >(Remark : in the catch part, I can manage to hind the
    >data of the column, but I cannot hide the header, and all
    >my data columns are shifted left)
    >Thanks in advance,
    >
    >------------- Code ------------------
    >Private Sub Page_Load(ByVal sender As System.Object,
    >ByVal e As System.EventArg s) Handles MyBase.Load
    > Dim msg As String
    > 'Put user code to initialize the page here
    > SqlConnection1. Open()
    > Dim rq As String = "select * from Categories"
    > Dim cmd As New SqlCommand(rq, SqlConnection1)
    > Dim dr As SqlDataReader = cmd.ExecuteRead er
    > Dim objItem As DataGridItem
    > Dim ThisBackColor As New System.Drawing. Color
    > DataGrid1.DataS ource = dr
    > DataGrid1.DataB ind()
    > Try
    > DataGrid1.Colum ns(0).Visible = False
    > Catch ex As Exception
    > For Each objItem In DataGrid1.Items
    > objItem.Cells(0 ).Visible = False
    > Next
    > End Try
    > End Sub
    >.
    >[/color]

    Comment

    • Thanh-Nu

      #3
      How to hide a column in datagrid with AutoGenerateCol =true

      Thank you very much!
      It works !!!
      [color=blue]
      >-----Original Message-----
      >declare a variable in your page.
      >int HideColumn;
      >
      >Turn the visibility of required column to Off on
      >ItemBound
      >
      > public void OnItemBound(Obj ect Sender,
      >DataGridItemEv entArgs e)
      >{
      > // Get the newly created item
      > int i;
      > ListItemType itemType = e.Item.ItemType ;
      >
      >if(HideColum n >=0 && HideColumn<e.It em.Controls.Cou nt)
      > e.Item.Cells[HideColumn].Visible = false;
      >}
      >[color=green]
      >>-----Original Message-----
      >>Hi,
      >>
      >>I would like to hide a column in a web datagrid (with
      >>create columns automatically at runtime checked), and I
      >>cannot refer to the columns collection like this:
      >>DataGrid1.Col umns(0).Visible = False
      >>(the message at runtime is something like index out of
      >>range, and under the debuger, I discover that the
      >>attribute count of the columns collection is 0!)
      >>Below is my piece of code.
      >>Could someone help me to solve this problem without
      >>having to bind the datagrid manually, because I have a
      >>sort of generic application, in which I cannot know in
      >>advance the column name.
      >>(Remark : in the catch part, I can manage to hind the
      >>data of the column, but I cannot hide the header, and[/color][/color]
      all[color=blue][color=green]
      >>my data columns are shifted left)
      >>Thanks in advance,
      >>
      >>------------- Code ------------------
      >>Private Sub Page_Load(ByVal sender As System.Object,
      >>ByVal e As System.EventArg s) Handles MyBase.Load
      >> Dim msg As String
      >> 'Put user code to initialize the page here
      >> SqlConnection1. Open()
      >> Dim rq As String = "select * from Categories"
      >> Dim cmd As New SqlCommand(rq, SqlConnection1)
      >> Dim dr As SqlDataReader = cmd.ExecuteRead er
      >> Dim objItem As DataGridItem
      >> Dim ThisBackColor As New System.Drawing. Color
      >> DataGrid1.DataS ource = dr
      >> DataGrid1.DataB ind()
      >> Try
      >> DataGrid1.Colum ns(0).Visible = False
      >> Catch ex As Exception
      >> For Each objItem In DataGrid1.Items
      >> objItem.Cells(0 ).Visible = False
      >> Next
      >> End Try
      >> End Sub
      >>.
      >>[/color]
      >.
      >[/color]

      Comment

      Working...