我正在努力使它如果特定列中的单元格不包含值,我希望该单元格改变颜色。
我目前没有任何可以显示的示例代码,但如果有人可以提供帮助,我将不胜感激。
答案 0 :(得分:1)
您的RowDataBound
事件应该是这样的
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text == "open")
{
e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
}
else if (e.Row.Cells[0].Text == "close")
{
e.Row.Cells[0].ForeColor = System.Drawing.Color.Black;
}
else
{
e.Row.Cells[0].ForeColor = System.Drawing.Color.Green;
}
}
}
答案 1 :(得分:0)
首先,您需要在标记中定义GridView
,如下所示:
<asp:GridView id="GridView1" emptydatatext="No data available." runat="server" onrowdatabound="GridView1_RowDataBound" >
<Columns>
<asp:boundfield datafield="CustomerID" headertext="Customer ID"/>
<asp:boundfield datafield="CompanyName" headertext="Company Name"/>
<asp:boundfield datafield="Address" headertext="Address"/>
<asp:boundfield datafield="City" headertext="City"/>
<asp:boundfield datafield="PostalCode" headertext="Postal Code"/>
<asp:boundfield datafield="Country" headertext="Country"/>
</Columns>
</asp:GridView>
注意:您的GridView
DataSource
需要具有与datafield
中定义的GridView
值相匹配的公共属性名称。
其次,您需要实施onrowdatabound
中定义的GridView
事件,该事件指向名为GridView1_RowDataBound
的方法,如下所示:
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Put logic here to check particular cell value
// Here is an example of changing the second cell (`Cells` collection is zero-based) to italic
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
}
}