.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

How to change the color of grid view row based on certain condition from database

Posted By: anil     Posted Date: May 22, 2012    Points:5   Category :ASP.Net
I am using gridview in my report page.
at page load,binded with data from two table ,i have display only few field from both table ,and when the user selected the select button in front of every row the other detail are displayed in formview.

now this application is for shipping company they want that if the ship has not reached on discharge port on time.means that if the ship is late,The record of this ship must be high lighted ,the problem is their are 7 drop down and 8 radio button list based on the selection of this control the data in gridview is change.

I some how done the code for getting the record of the ship who are not on time .But i dont know how to change the row color of this recod in grid,And i have written a code for each drop down selection and radio button selection ,whether i have to change all code or their are some other method to achieve this .

Below is the code which i am using to display the record of the ship from Load_port table and Discharge_port table

how to change the color of the row
at present i am using button and on the click event of the button i am displaying the result but client want that when he loged in he must see the record of the ship which are not on time

objdb = new DB();
string StrQ = "select DP.Ref_No,DP.Category_Name,DP.Appointed_By1,DP.Total_Qty,DP.DP1_Name,DP.DP1_SailingDate,LP.Vessel_Name,LP.Total_Lp_Qty,LP.LP1_Name,LP.Load_Sailing from Discharge_Port as DP inner join Load_Port as LP on DP.Ref_No =LP.Ref_No where DATEDIFF(Day,DP.ETA,GETDATE())>'12' and DP.DP1_ArrivalDate is null";
GvLpDpDetail.DataSource = objdb.getdata(StrQ);

please tell me how to achieve above result

Author: zohaib             
Posted Date: June 06, 2012     Points: 20   

if (e.Row.RowType.Equals(DataControlRowType.DataRow))
DataRowView drv = (DataRowView)e.Row.DataItem;
for (int i = 0; i < drv.DataView.Table.Columns.Count; i++)

if ((int)DataBinder.Eval(e.Row.DataItem, "RoleID") == 2)
e.Row.Cells[i].BackColor = System.Drawing.Color.LawnGreen;
else if ((int)DataBinder.Eval(e.Row.DataItem, "RoleID") == 1)
e.Row.Cells[i].BackColor = System.Drawing.Color.Red;
e.Row.Cells[i].BackColor = System.Drawing.Color.Aquamarine;

catch (Exception ex)

Try This code this will work for you RoleID is database Feild...

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend