【问题标题】:Conditional formatting on an entire column in GridviewGridview 中整列的条件格式
【发布时间】:2016-08-17 18:37:34
【问题描述】:

我需要根据单元格是否包含字符串“是”或“否”来格式化网格视图中的 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;
            }
        }
    }

【问题讨论】:

标签: c# asp.net


【解决方案1】:

查看此示例以获得一些方向。此代码位于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;
    }
}

【讨论】:

  • var valueFetched 中的 e.row.Cells[1] 指的是每一行的第一个单元格吗?
  • 是的,这是一个引用单元格的索引。您需要将该索引更改为列(索引从 0 开始);类型转换也是我的例子中的标签;您可能需要更改为该单元格中的控件
  • 将我所做的添加到我的问题中。我将表格单元格放入混淆标签以及我的 FindControl().text 中。我输入“不”是因为这就是我正在寻找的东西,但我对那是那里的东西还是其他东西感到困惑。我尝试了几种不同的方法,但都没有奏效。
  • 我的示例希望您对 GridView 有一些控制,其名称为 - yourControlId;如果您不提供有关 GridView 布局方式的完整信息,我将无法提供帮助。
猜你喜欢
  • 1970-01-01
  • 2012-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多