CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

How to Access Data Row Column data in ItemDataBound event of DataGridView, DataList and Repeater Control ?
Submitted By Bala Murugan
On 3/15/2010 10:23:40 AM
Tags: ASP.Net,C#,CodeDigest  

How to Access Data Row column data in ItemDataBound event of DataGridView, DataList and Repeater Control ?

 

Sometimes, we may require getting all or some column values of a data row in ItemDataBound event for some manipulations.

 

For example, there will be scenarios where we will not show the primary key field of table row to the users whenever we use the above databound controls to display data. In this case, if we want to access the primary key field or any column data in ItemDatabound event which is not shown to user but still available in the DataTable object or a collection that is binded to the databound controls, one can use the DataItem property.

 

The DataItem(e.Item.DataItem) in ItemDataBound event will return the data row with all column values of the current row.

 

Refer the code below,

 

GridView Control
protected void gvUsersBF_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = e.Row.DataItem as DataRowView;
                if (drv != null)
                {
            string id = drv.Row[0].ToString();
 }
        }
    }

 

DataList Control
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataRowView drv = e.Item.DataItem as DataRowView;
                if (drv != null)
                {
            string id = drv.Row[0].ToString();
}
        }
    }

 

Repeater Control
protected void rpErrorResolution_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {             
                Resolutions res = e.Item.DataItem as Resolutions;
                if (res != null)
                {
                    authorid = res.UserID.ToString();
                }
               
        }
    }

 

In the above code, GridView and DataList control are binded with DataTable object, hence every data row is casted to DataRowView. The Repeater control is binded with List< Resolutions>, hence every data row (DataItem) are casted to Resolutions object.


 

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..