Gridview中整个列的条件格式

本文关键字:条件 格式 Gridview | 更新日期: 2023-09-27 18:02:53

我需要根据单元格是否包含字符串"是"或"否"来格式化我的gridview中的2个完整列。我到处寻找,试图找到一些完成我想做的事情,不能找到任何基于字符串的东西,只有int值。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        GridView gv = (GridView)e.Row.FindControl("GridView2");
        var ds = new SqlDataSource();
        ds.ConnectionString = ConfigurationManager.ConnectionStrings["HUTDMSConnectionString"].ConnectionString;
        ds.SelectCommand = "SELECT * FROM textBooks WHERE CourseID = '" + GridView1.DataKeys[e.Row.RowIndex].Value + "' ORDER BY BookTitle";
        gv.DataSource = ds;
        gv.DataBind();
    }
}

我知道条件格式在gridview_rowdatabound中事件。

目前为止我有什么:

    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e) {
        if (e.Row.RowType == DataControlRowType.DataRow && ((e.Row.RowState & DataControlRowState.Edit) == 0))
        {
            var valueFetched = ((TableCell)(e.Row.Cells[3].FindControl("no"))).Text;
            if (valueFetched == "no")
            {
                foreach (var cell in e.Row.Cells)
                    ((TableCell)cell).BackColor = Color.Red;
            }
        }
    }

Gridview中整个列的条件格式

看一下这个例子可以得到一些指导。此代码属于RowDataBound

if (e.Row.RowType == DataControlRowType.DataRow && ((e.Row.RowState & DataControlRowState.Edit) == 0))
{
    var valueFetched = ((Label)(e.Row.Cells[1].FindControl("yourControlId"))).Text;
    if (valueFetched == "1")
    {
        foreach (var cell in e.Row.Cells)
            ((TableCell)cell).BackColor = System.Drawing.Color.LightBlue;
    }
}