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


Top 5 Contributors of the Month
david stephan

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

color row in gridview

Posted By: Suma     Posted Date: May 19, 2009    Points:2   Category :ASP.Net
Hi,
I need to color row in gridview in asp.net based on some condition. Suppose if salary is > 10,000 then the color of the row should be red if salary is >10,000 and less than 20,000 than it should be yellow else green.

i am using C# and asp.net

Please help me

Thanks in advance
Suma

Responses
Author: Preeti             
Posted Date: May 20, 2009     Points: 5   

Hello Suma,

To chage the row color you can write code in Grid View RowDataBound Event handler.
Here i have one Grid View that have two columns and i am changing row color according to your condition.I have created css file also.
Please find the code for that.


Write the below code in page not postback condition
if (!IsPostBack)
{
gvSample.RowDataBound += new GridViewRowEventHandler(gvSample_RowDataBound);
BindGridView();
}

//To bind Grid View with data table that has two column
//one column is just id column
//Second is salary column
//You can bind grid view with arraylist etc.
Private void BindGridView()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", Type.GetType("System.Int32"));
dt.Columns.Add("Salary", Type.GetType("System.Int64"));

DataRow dr = dt.NewRow();
dr[0] = "1";
dr[1] = "9999";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = "2";
dr[1] = "19000";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = "3";
dr[1] = "20000";
dt.Rows.Add(dr);

gvSample.DataSource = dt;
gvSample.DataBind();
}

Theb write below mentioned code to change the row color
void gvSample_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (int.Parse(e.Row.Cells[1].Text) < 10000)
e.Row.Attributes.Add("class", "GridVIewRowStyle_RedColor");
else if (int.Parse(e.Row.Cells[1].Text) > 10000 && int.Parse(e.Row.Cells[1].Text) < 20000)
e.Row.Attributes.Add("class", "GridVIewRowStyle_YellowColor");
else
e.Row.Attributes.Add("class", "GridVIewRowStyle_NoColor");
}
}
}

//Write the below code in the css file. //eg. Style.css
.GridVIewRowStyle_RedColor
{
border-right: #cccccc 0px solid;
border-top: black 0px solid;
padding-left: 8px;
font-size: 10px;
border-left: #cccccc 1px solid;
color: #333333;
border-bottom: #cccccc 1px solid;
font-family: Verdana;
background-color: Red;
text-decoration: none;
border-width: 1px;
border-color: black;
}
.GridVIewRowStyle_YellowColor
{
border-right: #cccccc 0px solid;
border-top: black 0px solid;
padding-left: 8px;
font-size: 10px;
border-left: #cccccc 1px solid;
color: #333333;
border-bottom: #cccccc 1px solid;
font-family: Verdana;
background-color: Yellow;
text-decoration: none;
border-width: 1px;
border-color: black;
}
.GridVIewRowStyle_NoColor
{
border-right: #cccccc 0px solid;
border-top: black 0px solid;
padding-left: 8px;
font-size: 10px;
border-left: #cccccc 1px solid;
color: #333333;
border-bottom: #cccccc 1px solid;
font-family: Verdana;
background-color: White;
text-decoration: none;
border-width: 1px;
border-color: black;
}

Write this piece of code in .aspx file and include style.css file here.
<link href="Style.css" type="text/css" rel="Stylesheet" />
<asp:GridView ID="gvSample" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Id" DataField="Id" />
<asp:BoundField HeaderText="Salary" DataField="Salary" />
</Columns>
</asp:GridView>


i hope that my answer is close to your question.

Author: Athira Sreejith             
Posted Date: May 20, 2009     Points: 5   

HI..


protected void gvTourProgram_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow gvRow = e.Row;
for (int i = 3; i < gvTourProgram.Columns.Count; i++)
{
if (i != 5)
{
gvRow.Cells[i].Text = string.Empty;
}
else
{
gvRow.Cells[i].ForeColor = System.Drawing.Color.Red;
gvRow.Cells[i].Font.Bold = true;
gvRow.Cells[i].Font.Size = 10;
gvRow.Cells[i].Text = "SUNDAY";
}
}
}
}



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
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

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