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

Determining the type of a column

3 Answers 314 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Norman
Top achievements
Rank 1
Norman asked on 13 Dec 2010, 07:56 AM
Hi guys,

I have a RadGrid which is populated dynamically by SQL queries at runtime.  I would like to add a total field for numeric columns in the footer, but since the column can be anything potentially, how do I programmatically determine the type of this column at runtime?  I'm aggregating the total in the RadGrid's ItemDataBound event and looking to potentially storing the total for each relevant column in a hash table (using the column name as the key).

Cheers,
Norman

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 13 Dec 2010, 08:23 AM
Hello Norman,

Check out the following code snippet which shows how to find out the type of column and how to access its column UniqueName.

C#:
foreach (GridColumn col in RadGrid1.Columns)
       {
           if (col is GridNumericColumn)// checking for type of column
           {
              col.UniqueName//accessing column UniqueName
           }
       }

Thanks,
Princy.
0
Norman
Top achievements
Rank 1
answered on 13 Dec 2010, 11:45 PM
Hi Princy,

Cheers for the help. I'm not entirely sure how your code listing will help me in this case.  Here's a snippet of code along the lines of what I was thinking of doing, which will hopefully clarify things:

Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
    If TypeOf e.Item Is GridDataItem Then
        ' If our field is of the right type, aggregate total for this column
        Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)
        If ' Check if numeric...
' Add to total for this column
            totals(dataItem.ItemIndex).AddToTotal(' Converted type)
        End If
    ElseIf TypeOf e.Item Is GridFooterItem ' And numeric...
        ' If our field is of the right type, display a total for this column
        Dim footerItem As GridFooterItem = CType(e.Item, GridFooterItem)
        ' Read from total for this column
        footerItem("Total").Text = totals(footerItem.ItemIndex).ToString()
    End If
End Sub

Thanks for your help!
    Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
        If TypeOf e.Item Is GridDataItem Then
            ' If our field is of the right type, aggregate total for this column
            Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)
            If TypeOf dataItem.DataItem Is Integer Then
                totals(dataItem.ItemIndex).AddToTotal(CType(dataItem.DataItem, Integer))
            ElseIf TypeOf e.Item.DataItem Is Double Then
                totals(dataItem.ItemIndex).AddToTotal(CType(dataItem.DataItem, Double))
            End If
        ElseIf TypeOf e.Item Is GridFooterItem And (TypeOf e.Item.DataItem Is Integer Or TypeOf e.Item.DataItem Is Double) Then
            ' If our field is of the right type, display a total for this column
            Dim footerItem As GridFooterItem = CType(e.Item, GridFooterItem)
            footerItem("Total").Text = totals(footerItem.ItemIndex).ToString()
        End If
    End Sub
0
Veli
Telerik team
answered on 16 Dec 2010, 01:24 PM
Hi Norman,

Isn't it easier to just get the original data item that was used to bind the current GridDataItem instance?

Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
    If TypeOf e.Item Is GridDataItem Then
        Dim gridItem As GridDataItem = e.Item
        Dim sourceItem as Object = gridItem.DataItem
        'sourceItem is the original data source item used to bind the current
        'GridDataItem. You can cast it to the correct type (e.g. DataRowView)
        'and use its value for aggregation
    End IF
End Sub

This way, you can extract data values directly. If not, you need to parse the cell Text to the correct type in item cells:

Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
    If TypeOf e.Item Is GridDataItem Then
        Dim gridItem As GridDataItem = e.Item
        Dim numValStr As String = gridItem("NumericColumnName").Text
        Dim numVal As Double = Double.Parse(numValStr)
    End IF
End Sub


Veli
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Norman
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Norman
Top achievements
Rank 1
Veli
Telerik team
Share this question
or