Datagrid Dynamic Columns

Discussion in 'ASP .Net Datagrid Control' started by Paul Say, Aug 19, 2004.

  1. Paul Say

    Paul Say Guest

    This is my problem

    I have a data grid with about twenty columns, I have a dropdown list that
    allows the user to select the view they want for the datagrid, based on the
    selection the various columns visible property is toggled.

    This has been working well. What i want to know is if there is any way to
    change the order that the coumns appear in.

    I tries the following code which works If a sequential number of columns are
    selected (1,3,2,4 - this is ok as all columns from 1 to 4 are represented)
    (1,3,4) this fails as column 2 is missing)

    'Set data grid columns visibility
    Sub DataGrid1_ShowColumns()
    'show columns as per selected view
    dgNew.Columns.Clear()

    Select Case ddGridView.Items(ddGridView.SelectedIndex).Value
    Case "Job Name"
    ShowColumn(1)
    ShowColumn(3)
    ShowColumn(2)
    ShowColumn(4)
    Case "Job Name2"
    ShowColumn(1)
    ShowColumn(3)
    ShowColumn(4)
    Case Else
    ShowColumn(1)
    ShowColumn(2)
    ShowColumn(3)
    End Select

    DataGrid1.Columns.Clear()
    For Each dc As DataGridColumn In dgNew.Columns
    DataGrid1.Columns.Add(dc)
    Next
    End Sub

    Private Sub ShowColumn(ByVal col As Integer)
    Dim dgc As DataGridColumn = DataGrid1.Columns(col)
    dgNew.Columns.AddAt(dgNew.Columns.Count, dgc)
    End Sub

    This is the error that is created

    Index was out of range. Must be non-negative and less than the size of the
    collection. Parameter name: index
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.ArgumentOutOfRangeException: Index was out of
    range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    Source Error:

    Line 132: End Sub
    Line 133: Private Sub ShowColumn(ByVal col As Integer)
    Line 134: Dim dgc As DataGridColumn = DataGrid1.Columns(col)
    Line 135: dgNew.Columns.AddAt(dgNew.Columns.Count, dgc)
    Line 136: End Sub

    Source File: c:\inetpub\wwwroot\DrwReg\Jobs\JobList.aspx.vb Line: 134

    Stack Trace:

    [ArgumentOutOfRangeException: Index was out of range. Must be non-negative
    and less than the size of the collection.
    Parameter name: index]
    System.Collections.ArrayList.get_Item(Int32 index) +91
    System.Web.UI.WebControls.DataGridColumnCollection.get_Item(Int32 index)
    +10
    JobList.ShowColumn(Int32 col) in
    c:\inetpub\wwwroot\DrwReg\Jobs\JobList.aspx.vb:134
    JobList.DataGrid1_ShowColumns() in
    c:\inetpub\wwwroot\DrwReg\Jobs\JobList.aspx.vb:114
    JobList.ddGridView_SelectedIndexChanged(Object sender, EventArgs e) in
    c:\inetpub\wwwroot\DrwReg\Jobs\JobList.aspx.vb:275
    System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e)
    +108

    System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.Ra
    isePostDataChangedEvent() +26
    System.Web.UI.Page.RaiseChangedEvents() +115
    System.Web.UI.Page.ProcessRequestMain() +1081
     
    Paul Say, Aug 19, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J.B
    Replies:
    1
    Views:
    2,639
    Natty Gur
    Oct 23, 2003
  2. Jordo
    Replies:
    0
    Views:
    470
    Jordo
    Oct 31, 2003
  3. avnrao
    Replies:
    3
    Views:
    3,712
    Elad Volpin
    Jun 6, 2004
  4. rob thomson
    Replies:
    0
    Views:
    283
    rob thomson
    Sep 4, 2003
  5. ton
    Replies:
    2
    Views:
    218
    Alvin Bruney [MVP]
    Feb 11, 2004
Loading...

Share This Page