0

我正在尝试这样做,以便如果特定列中有一个不包含值的单元格,我希望该单元格改变颜色。

我目前没有任何可以显示的示例代码,但如果有人能提供帮助,我将不胜感激。

4

2 回答 2

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;
            }
        }
    }
于 2013-08-02T13:01:00.370 回答
0

首先,您需要定义一个GridViewin 标记,如下所示:

<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>

注意:您GridViewDataSource需要具有与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>";
    }
}
于 2013-08-02T11:07:35.397 回答