Gridview中整个列的条件格式设置

时间:2016-08-17 18:37:34

标签: c# asp.net

我需要在gridview中格式化2个完整列,具体取决于单元格是否包含字符串" yes"或"不"。我到处都试图找到一些东西来完成我想要做的事情,并且无法根据字符串找到任何东西,只有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;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

看看这个例子来获得一些方向。此代码位于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;
    }
}
相关问题