简体   繁体   中英

Formatting programmatically changing the row color of a GridView control

I have a GridView control that reads from a stored procedure in a SQL Server 2008 database and I am trying to change the backgound color of a row is a certain cell contains certain text. For whatever reason, the solution that I've come up with only works if there is more than one row in the GridView. If there is only one row in the GridView, then the row color of that row isn't changed. I'll post more code if needed, but I'm using the OnRowDataBound event of the GridView control.

c# for the OnRowDataBound event

 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;

        }
    }

}

Here is the structure of the GridView control

columnName1   column1Desc   columnName2   column2Desc
one           C6N           two           zzz

The intent is to change the row color of the GridView if column1Desc is equal to C6N

Below is the button click that binds the GridView. Underneath if(rdr.HasRows) I wrote to the response object and it is in fact printing the correct value of C6N, but when the result set is only one row, the formatting change doesn't take place.

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");
            } 

        }

First thing - you don't need to loop through your gridview rows because this method gets called with EVERY row anyway.

You already know the row you want to query as it just called this method and passed information via the GridViewRowEventArgs property.

So something like this may suit better:

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

Also - are you using a template to house the text you're checking? Because if you are, you need to get the control that the text is in - rather than just Cell(1).Text - as this will return all manner of things OTHER than your text.

EG if you have the text in a label, then check the text of the label instead, by using this instead

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM