I'm trying to figure out how to dynamically add columns to a hierarchical/nested grid in a RadGrid. Which columns need to be created are determined by a Check Box List control. And I only know for sure which columns to add after the RadGrid_DetailTableDataBind is called.
I can get it all to work if I set AutoGenerateColumns to TRUE. But of course I need to make two of the columns (if they are selected) TemplateColumns. Which is why I have to dynamically add the columns.
To me it looks like I have two choices (neither of which I could make work). 1) Add the columns to a statically created hierarchical grid. Or 2) Create the hierarchical grid dynamically and then the columns.
For the first option after the RadGrid_DetailTableDataBind is invoked, a datatable is populated from a database query. After the datatable (which is going to be the datasource) is generated I call the following routine (with datasource table as the one calling parameter).
I can post the code for DataGridTemplate, but I don't think that's the problem here. I suspect that I'm getting something more fundamental wrong.
The results I get when running this code is I do get a grid row for each record in my results. Their headers are blank and the contents of the GridBoundColumn are: System.Data.DataRowView. The template columns are blank.
My attempt at the 2nd approach was even less successful. So I'll need more direction if this turns out to be the best way to go.
Thanks for any help on this.
I can get it all to work if I set AutoGenerateColumns to TRUE. But of course I need to make two of the columns (if they are selected) TemplateColumns. Which is why I have to dynamically add the columns.
To me it looks like I have two choices (neither of which I could make work). 1) Add the columns to a statically created hierarchical grid. Or 2) Create the hierarchical grid dynamically and then the columns.
For the first option after the RadGrid_DetailTableDataBind is invoked, a datatable is populated from a database query. After the datatable (which is going to be the datasource) is generated I call the following routine (with datasource table as the one calling parameter).
Sub
AddColumnsToGrid(
ByVal
ResultTable
As
DataTable)
Dim
bc
As
GridBoundColumn
Dim
dgOSEvent
As
GridTableView = RadGrid1.MasterTableView.DetailTables(0)
dgOSEvent.Columns.Clear()
'Dim dgOSEvent As GridTableView = New GridTableView(RadGrid1)
bc =
New
GridBoundColumn()
bc.DataField =
"% of Total"
bc.HeaderText =
"% of Total"
bc.ItemStyle.HorizontalAlign = HorizontalAlign.Center
bc.DataFormatString =
"{0:p}"
dgOSEvent.Columns.Add(bc)
bc =
New
GridBoundColumn()
bc.DataField =
"Count"
bc.HeaderText =
"Event Count"
bc.ItemStyle.HorizontalAlign = HorizontalAlign.Center
bc.DataFormatString =
"{0:###,###,###}"
dgOSEvent.Columns.Add(bc)
For
Each
dc
As
DataColumn
In
ResultTable.Columns
If
dc.ColumnName <>
"COUNT"
And
dc.ColumnName <>
"% of Total"
Then
If
dc.ColumnName =
"SOURCE"
Or
dc.ColumnName =
"DESTINATION"
Then
Dim
tc
As
GridTemplateColumn =
New
GridTemplateColumn()
tc.HeaderTemplate =
New
DataGridTemplate(ListItemType.Header, dc.ColumnName)
tc.ItemTemplate =
New
DataGridTemplate(ListItemType.Item, dc.ColumnName)
tc.ItemStyle.HorizontalAlign = HorizontalAlign.Center
dgOSEvent.Columns.Add(tc)
Else
bc =
New
GridBoundColumn()
bc.DataField = dc.ColumnName
bc.HeaderText = dc.ColumnName
If
dc.ColumnName =
"Description"
Then
bc.ItemStyle.HorizontalAlign = HorizontalAlign.Left
Else
bc.ItemStyle.HorizontalAlign = HorizontalAlign.Center
End
If
dgOSEvent.Columns.Add(bc)
End
If
End
If
Next
End
Sub
I can post the code for DataGridTemplate, but I don't think that's the problem here. I suspect that I'm getting something more fundamental wrong.
The results I get when running this code is I do get a grid row for each record in my results. Their headers are blank and the contents of the GridBoundColumn are: System.Data.DataRowView. The template columns are blank.
My attempt at the 2nd approach was even less successful. So I'll need more direction if this turns out to be the best way to go.
Thanks for any help on this.