【问题标题】:Formatting programmatically changing the row color of a GridView control以编程方式格式化更改 GridView 控件的行颜色
【发布时间】:2013-05-28 18:42:39
【问题描述】:

我有一个 GridView 控件,它从 SQL Server 2008 数据库中的存储过程中读取数据,并且我正在尝试更改某行的背景颜色是某个单元格包含某些文本。无论出于何种原因,我提出的解决方案仅在 GridView 中有不止一行时才有效。如果 GridView 中只有一行,则该行的行颜色不会更改。如果需要,我会发布更多代码,但我使用的是OnRowDataBound GridView 控件的事件。

OnRowDataBound 事件的 c#

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        //in every row I would like to check the text of the second cell
        if (GridView1.Rows[i].Cells[1].Text == "C6N")
        {
            e.Row.BackColor = System.Drawing.Color.Red;

        }
    }

}

这里是GridView控件的结构

columnName1   column1Desc   columnName2   column2Desc
one           C6N           two           zzz

目的是在 column1Desc 等于 C6N 时更改 GridView 的行颜色

下面是绑定 GridView 的按钮单击。在if(rdr.HasRows) 下面我写了响应对象,它实际上打印了正确的 C6N 值,但是当结果集只有一行时,格式更改不会发生。

protected void Button2_Click(object sender, EventArgs e)
    {
        //craete the drugString variable
        string drugString = string.Join(string.Empty,druglist.ToArray()).TrimEnd(',');

        //create the connection string
        string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
        //create the connection object 
        using (SqlConnection con = new SqlConnection(cs))
        {
            //create the command object
            using (SqlCommand cmd = new SqlCommand("spMakeTempTable2", con))
            {
                //don't forget to open the connection
                con.Open();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@drugList", drugString);
                SqlDataReader rdr = cmd.ExecuteReader();
                GridView1.DataSource = rdr;
                GridView1.DataBind();
                if (rdr.HasRows)
                    Response.Write(GridView1.Rows[0].Cells[1].Text);
                //this line prints to the screen as it should
                else
                    Response.Write("There were no drug combinations present");
            } 

        }

【问题讨论】:

  • 您是否进行了测试以确保OnRowDataBound 事件被触发?
  • @MikePrecup 只学习 C#/asp.net,我将如何测试?
  • @MikePrecup 我在其中设置了一个断点并进行了调试,但它们的事件从未触发过。

标签: c# asp.net visual-studio-2010 gridview


【解决方案1】:

第一件事 - 你不需要遍历你的 gridview 行,因为这个方法无论如何都会被每一行调用。

您已经知道要查询的行,因为它只是调用此方法并通过GridViewRowEventArgs 属性传递信息。

所以这样的东西可能更适合:

GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    // you only want to check DataRow type, and not headers, footers etc.
    if (e.Row.RowType == DataControlRowType.DataRow) {
            // you already know you're looking at this row, so check your cell text
        if (e.Row.Cells(1).Text == "C6N") {
            e.Row.BackColor = System.Drawing.Color.Red;
        }
    }
}

另外 - 您是否使用模板来存放您正在检查的文本?因为如果你是,你需要控制文本所在的位置——而不仅仅是Cell(1).Text——因为这将返回除你的文本之外的所有东西。

例如,如果标签中有文本,则改为检查标签的文本,使用 this 代替

Label lbl = (Label)e.Row.FindControl("myTextLabel");

if (lbl.Text == "C6N") {
    e.Row.BackColor = System.Drawing.Color.Red;
}

【讨论】:

  • 感谢提醒,我不知道会为 GridView 中的每一行调用此方法。这看起来是我需要的。 Mil gracias :)
猜你喜欢
  • 1970-01-01
  • 2015-12-06
  • 2021-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-21
相关资源
最近更新 更多