Greetings Gurus. I have a gridview who's rows I need to higlight if the 'Status' field is null. The code below throws an exception. I'm getting a "Non-invocable member 'System.Web.UI.WebControls.GridViewRow.DataItem' cannot be used like a method". I think I'm really close but I don't know where to turn next.
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow &&
e.Row.DataItem("Status") == null)
{
// e.Row开发者_如何学JAVA.BackColor = Drawing.Color.Red;
}
}
basic
CType(e.Row.DataItem, DataRowView)("Status") = null
c#
((DataRowView)e.Row.DataItem)["Status"] == null
or do it in more than one line -- see example here: msdn example
I haven't looked at this in a long while but do you access DataItem like an array??
e.Row.DataItem("Status") would be e.Row.DataItem["Status"]
Look here
// Retrieve the underlying data item. In this example // the underlying data item is a DataRowView object. DataRowView rowView = (DataRowView)e.Row.DataItem;
// Retrieve the state value for the current row. String state = rowView["state"].ToString();
Try casting:
if (((MyType)e.Row.DataItem).Status == null) { ... }
Have a look at the sample code at MSDN.
The key points are:
Cast your DataRow to a DataRowView, like this:
DataRowView rowView = (DataRowView)e.Row.DataItem;
You can then retrieve values like this:
// Retrieve the status value for the current row.
Status status = (Status)rowView["status"];
...or cast to whatever type Status happens to be.
Got it. It may not be the best solution but it works.
if (e.Row.RowType == DataControlRowType.DataRow )
{
DataRowView test = (System.Data.DataRowView)e.Row.DataItem;
string Active = test.Row[4].ToString();
if(Active == "")
{
e.Row.BackColor = System.Drawing.Color.Sienna;
e.Row.ForeColor = System.Drawing.Color.Yellow;
}
}
精彩评论