System.Web.UI.WebControls Namespace GridViewRow Class
.NET Framework version 2.0
Sets or retrieves the row of data associated with a GridViewRow.
Script |
GridViewRow.DataItem [ = var ] |
var |
Object representing the underlying data row to which the GridViewRow is bound. |
The property is read/write with no default value.
NOTE: This property applies only to data rows in the GridView control.
Use the DataItem property to specify or determine the properties of the data row associated with a GridViewRow object in a GridView control.
DataItem is mainly used to retrieve field values from the row in the data source associated with a GridViewRow.
NOTE: The DataItem property is available only during the RowDataBound event of a GridView control, and is discarded after.
The following example demonstrates how to use the DataItem property to retrieve a field value in a GridViewRow.
- Create a handler for the RowDataBound event.
void setBindings ( Object src, GridViewRowEventArgs e ) {
...
}
- In the event handler, do the following:
- Test for a data row to make sure that you are working with a data-bound row and not a header or footer.
if ( e.Row.RowType == DataControlRowType.DataRow ) {
...
}
- Create a DataRowView object ( when data source mode is a DataSet ) or an IDataRecord object ( when data source mode is a DataReader ) and set its value to the DataItem for the current grid row.
DataRowView rowView = ( DataRowView ) e.Row.DataItem;
NOTE: The DataItem property is typed as an object, which must be cast to the appropriate type.
- From the DataRowView object, extract the data value you need to examine.
if ( rowView [ "url" ].ToString ( ).IndexOf ( "demos" ) > -1 )
GridViewRow Members DataItemIndex RowIndex