0

I am making a application in asp.net which shows the values in gridview from the database.In database i am having a colmn named as StatusId which has a value of 1 or 2 or 3.

I tried to show the grid view rows in different color by their statusId values. But it never works. How can i do it in asp.net.

Here is my code

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        Connection.Open();
        SqlCommand Command1 = Connection.CreateCommand();
        Command1.CommandText = "Select statusColor from status where statusId=(Select statusId from fileInfo where userId=(Select userId from userInfo where email='" + Session["email"].ToString() + "'))";

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            using (SqlDataReader reader = Command1.ExecuteReader())
            {
                while (reader.Read())
                {
                    statusId = reader["statusColor"].ToString();
                }
             GridView1.RowStyle.BackColor = Color.FromName(statusId);           
        } 
        }


        foreach (GridViewRow row in GridView1.Rows)
        {
            row.BackColor = Color.Green;
        }
        SqlCommand com = new SqlCommand("gridcolor", Connection);
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.AddWithValue("@statusId", statusId);
        com.Parameters.Add("@statusColor", SqlDbType.NVarChar, 30);
        com.Parameters["@statusColor"].Direction = ParameterDirection.Output;
        com.ExecuteNonQuery();
        string msg = (string)com.Parameters["@statusColor"].Value;
        Connection.Close();
    } 

What is the mistake i am doing here?

EDIT

I have the color codes which are stored in the database named as statusColor. I have to apply those color to these status.

Vivek Dragon
  • 2,218
  • 4
  • 27
  • 48
  • 2
    Why don't you just return the status color as part of the data to which the grid is bound? Your current approach will cause a database call for *each row*. – Tim M. Apr 05 '13 at 05:11
  • Yeah i think that will be better way to change color for each row. – Vivek Dragon Apr 05 '13 at 05:12

3 Answers3

0

You have statusId having values 1,2,3 and you are passing to Color.FromName which and 1,2,3 are not names of color you can use switch to assign different colors based on statusId .

Color rowColor = Color.Red;    
switch(statusId)
{
    case 1:
         rowColor = Color.Green;
         break;

    case 2:
         rowColor = Color.White;
         break;
    case 3:
         rowColor = Color.Blue;
         break;
}   
GridView1.RowStyle.BackColor = rowColor ; 
Adil
  • 146,340
  • 25
  • 209
  • 204
0

You are doing it the wrong way. You have to databind your gridview on either page load or a custom event (say click of a button) & not rowDataBound event. This event occurs when your row is bound with data & you want to change some attributes for each row (as in your case).

You can use Gridview DataBound event to assign colors in following way

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Fetch status Id for current row
        int statusId= Convert.ToInt32(DataBinder.Eval(e.Row.DataItem,"UnitsInStock"));

        switch (statusId)
       {
         case (1):
                   e.Row.BackColor= System.Drawing.Color.Green;
                   break;

         // other cases follow the same
       }

    }
}

Put the databinding code in either pageLoad or button click event.

Zo Has
  • 12,599
  • 22
  • 87
  • 149
0
protected void grdMyQ_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            for (int i = 0; i < grdMyQ.Rows.Count; i++)
            {
                if (grdMyQ.Rows[i].Cells[13].Text.ToUpper() == "DISCHARGED_PROCESS")
                {
                    grdMyQ.Rows[i].BackColor = Color.Red;

                }
            }
        }
J. Ghyllebert
  • 2,009
  • 1
  • 29
  • 35