Dynamic Columns

P

Paul Say

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top