How to access datasource fields in an ASP.NET Repeaters ItemDataBound event?

Peter picture Peter · May 6, 2009 · Viewed 57.9k times · Source

I have a Repeater control that is being bound to the result of a Linq query.

I want to get the value of one of the datasource's fields in the ItemDataBound event, but I'm not sure how to do this.

Answer

Sprotch picture Sprotch · Feb 19, 2010

Depending on the DataSource... If your DataSource is a DataTable, then your DataItem contains a DataRowView:

protected void rptMyReteater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Button b = e.Item.FindControl("myButton") as Button; 
        DataRowView drv = e.Item.DataItem as DataRowView;
        b.CommandArgument = drv.Row["ID_COLUMN_NAME"].ToString();
    }
}