This is a migrated thread and some comments may be shown as answers.

How to dynamically add columns to a hierarchical grid

1 Answer 120 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Hunter
Top achievements
Rank 1
Hunter asked on 05 Apr 2013, 11:37 PM
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).

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.


1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 08 Apr 2013, 04:54 AM
Hi,

Check the following demo which implements the same.
Grid - Creating Hierarchy Programmatically

Thanks,
Shinu
Tags
Grid
Asked by
Hunter
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or