Programmatically Change Text Property of Label in ItemTemplate in Datagrid

C

Craig

How do I programmatically change the text property of a label in an
ItemTemplate in a datagrid?
Specifically the Text property. I want to change the databinding to another
column at runtime.

<asp:TemplateColumn HeaderText="Rate1">
<ItemTemplate>
<asp:Label runat="server" Text='<%# fRate(DataBinder.Eval(Container,
"DataItem.RateAgentID")) %>' ID="Label3" NAME="Label3">
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>

Thanks
 
K

Karl

Hook into the ItemDataBound event of the datagrid, then:

Label lbl = (Label)e.Item.FindControl("Label3")
if (lbl != null){
lbl.Text = ((DataRowView)e.Item.DataItem)["OtherColumn"]
}

DataItem actually returns an object which will be a DataRowView when binding
to a dataset/datatable/datarow...looks like you are binding to a custom
collection in which case it'll return your collection item type:

lbl.Text = ((YourClassEntity)e.Item.DataItem).YourProperty

Karl
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top