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

set grid template item value from code behind

1 Answer 106 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Clive Hoggar
Top achievements
Rank 1
Clive Hoggar asked on 01 Jul 2010, 03:03 PM
Hi

I am having a struggle getting this to work...
A grid item looks like this:

<telerik:GridTemplateColumn DataField="PricePerBottle"   
DataType="System.Decimal" HeaderText="£/Unit" SortExpression="PricePerBottle"   
UniqueName="PricePerBottle">  
<ItemTemplate> 
   <asp:Label ID="PricePerBottleLabel" runat="server" Text=''></asp:Label> 
</ItemTemplate> 
 
and I am trying to set  values from code behind in the item databound event:

Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemDataBound  
        If TypeOf e.Item Is GridDataItem Then  
              
                Dim label1 As Label = DirectCast(item("PricePerBottle").FindControl("PricePerBottleLabel"), Label)  
                  
                label1.Text = someMathFunction(item("PricePerBottle").Text) 

I am getting run time errors such as can't convert string "" to decimal. I have tried a number of variations but it seems I am not getting the column value.

Can you put me right?

Thanks

Clive

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 02 Jul 2010, 08:04 AM
Hello Clive,

Here is code snippet to access column value for the corresponding row.

VB:
 
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs) 
    If TypeOf e.Item Is GridDataItem Then 
        Dim row As DataRowView = DirectCast(e.Item.DataItem, DataRowView) 
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem) 
        Dim label1 As Label = DirectCast(item("PricePerBottle").FindControl("PricePerBottleLabel"), Label) 
        label1.Text = row("Freight").ToString() 
    End If 
End Sub 
 


-Shinu.
Tags
Grid
Asked by
Clive Hoggar
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or