Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. I have this in my page
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            onrowdatabound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="ProductName" HeaderText="Product Name"
                    SortExpression="ProductName" />
                <asp:BoundField DataField="Quantity" HeaderText="Quantity"
                    SortExpression="Quantity" />
                <asp:BoundField DataField="CriticalLevel" HeaderText="Critical Level"
                    SortExpression="CriticalLevel" />
            </Columns>
        </asp:GridView>


I want to change the color of the Row if the value of Quantity is less than Critical Level to Red etc etc. I tried this code

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
          DataRow pr =   ((DataRowView)e.Row.DataItem).Row;
          int id = Convert.ToInt32(pr["Quantity"]);
           if(id==1)
               e.Row.Cells[2].BorderColor = System.Drawing.Color.Red;
           if(id < 1)
               e.Row.Cells[2].BorderColor = System.Drawing.Color.Blue;
           if(id==0)
               e.Row.Cells[2].BorderColor = System.Drawing.Color.Violet;
       }
   }


I need help. Thank you!!
Posted
Comments
Hetal Jariwala 19-Mar-13 2:47am    
so whats problem??
BeastMode10 19-Mar-13 2:57am    
Hi. I just copied that source code. And I want to compare the cell values of column "Quantity" and "Critical Level" e.g. Quantity < Critical Level. And i want that cell to turn Red.
Hetal Jariwala 19-Mar-13 3:01am    
use pr["CriticalLevel"] to get critical level as you have used Quantity
BeastMode10 19-Mar-13 3:27am    
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow pr = ((DataRowView)e.Row.DataItem).Row;
int id = Convert.ToInt32(pr["Quantity"]);
int id2 = Convert.ToInt32(pr["CriticalLevel"]);
if (id == id2)
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
if (id < id2)
e.Row.Cells[2].BackColor = System.Drawing.Color.Blue;
if (id > id2)
e.Row.Cells[2].BackColor = System.Drawing.Color.Violet;
}

This is the new code and here is the error

"Object cannot be cast from DBNull to other types."
Hetal Jariwala 19-Mar-13 4:19am    
then first check
if pr["CriticalLevel"]!=DbNull.value
then convert to Int32

1 solution

Hi,

use this code in row data bound function

for each row you can assign background color

VB
e.Row.BackColor = Drawing.Color.LightBlue
e.Row.ForeColor = Drawing.Color.Black
 
Share this answer
 
Comments
BeastMode10 19-Mar-13 2:53am    
Is it possible to compare the values of columns "Quantity" and "Critical Level"? After comparing them I want to change the background color.
navi_s_Patil 19-Mar-13 3:28am    
yes you can compare columns in rowdatabound function

Quantity= DirectCast(e.Row.DataItem, System.Data.DataRowView).Row.ItemArray(14)
Critical = DirectCast(e.Row.DataItem, System.Data.DataRowView).Row.ItemArray(10)


using this you ll get value of that column then you can campare the values.

this code is in vb.net

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900