.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

Gridview Conditional Color Change

Posted By: xack     Posted Date: April 28, 2011    Points:5   Category :ASP.Net
Hi,

I'm doin a ToDo list and i want to make datecheck for previously and new entered data if deadline date is today i want that row backcolor is yellow if deadline passed red if both not nothing... i'm using access db i have a job_complete true/false in db and job_deadline for datetime. and when i open it i need to check exist contents date's for color change.

i've this button when pressed it changes color to green means completed job. it works, now i need to add these specific scenerios to my page, how can i do it, any help will be wonderful. thnx.

<asp:GridView ID="GridView1" runat="server" DataKeyNames="job_id" AutoGenerateColumns="False"
BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px"
CellPadding="4" GridLines="Horizontal" OnRowCommand="GridView1_RowCommand" AllowPaging="True"
OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lbljobID" runat="server" Text='<%# Eval("job_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Yapilacak Is">
<ItemTemplate>
<asp:Label ID="lbljob" runat="server" Text='<%# Eval("job") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Deadline">
<ItemTemplate>
<asp:Label ID="lbljob_deadline" runat="server" Text='<%# Eval("job_deadline","{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" Value='<%# Eval("job_complete") %>' runat="server" />
<asp:Button ID="btnyapilacak" runat="server" CommandName="Yapilacak" CommandArgument='<%# Eval("job_id") %>'
Text="Yapildi" />
<asp:Button ID="btnsil" runat="server" CommandName="Sil" CommandArgument='<%# Eval("job_id") %>'
Text="Sil" />
</ItemTemplate>
</asp:TemplateField>
</Columns>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "job_complete")))
{
e.Row.BackColor = Color.LightGreen;
}


}
}

Responses
Author: Mahesh             
Posted Date: May 04, 2011     Points: 20   

Hi,
you can change the row back color in RowDataBound event of the gridview control, please folloe the code below,


protected void booksGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;

if (row.RowType == DataControlRowType.DataRow & row.RowState == DataControlRowState.Normal || row.RowType == DataControlRowType.DataRow & row.RowState == DataControlRowState.Alternate)
{
Label authorLabel = (Label)row.Cells[1].FindControl("authorLabel");

if (authorLabel.Text == "John Grisham") //put u r condition
{
e.Row.BackColor = System.Drawing.Color.Wheat;
}
}
}



hope this may help u...

Thanks
Mahesh

Author: Maverick             
Posted Date: May 04, 2011     Points: 20   

Hello,

Use this

if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == 0)
{
e.Row.Style.Add("background-color", "#FF2525");
e.Row.Cells[0].BackColor = System.Drawing.Color.Red;
e.Row.Cells[1].BackColor = System.Drawing.Color.Red;
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
e.Row.Cells[3].BackColor = System.Drawing.Color.Red;
e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
e.Row.Cells[5].BackColor = System.Drawing.Color.Red;
e.Row.Cells[6].BackColor = System.Drawing.Color.Red;
}

else if (e.Row.RowIndex == 1)
{
e.Row.Style.Add("background-color", "orange");
e.Row.Cells[0].BackColor = System.Drawing.Color.Orange;
e.Row.Cells[1].BackColor = System.Drawing.Color.Orange;
e.Row.Cells[2].BackColor = System.Drawing.Color.Orange;
e.Row.Cells[3].BackColor = System.Drawing.Color.Orange;
e.Row.Cells[4].BackColor = System.Drawing.Color.Orange;
e.Row.Cells[5].BackColor = System.Drawing.Color.Orange;
e.Row.Cells[6].BackColor = System.Drawing.Color.Orange;
}
else if (e.Row.RowIndex == 2)
{
e.Row.Style.Add("background-color", "yellow");
e.Row.Cells[0].BackColor = System.Drawing.Color.Yellow;
e.Row.Cells[1].BackColor = System.Drawing.Color.Yellow;
e.Row.Cells[2].BackColor = System.Drawing.Color.Yellow;
e.Row.Cells[3].BackColor = System.Drawing.Color.Yellow;
e.Row.Cells[4].BackColor = System.Drawing.Color.Yellow;
e.Row.Cells[5].BackColor = System.Drawing.Color.Yellow;
e.Row.Cells[6].BackColor = System.Drawing.Color.Yellow;
}
}
}

Hope it helps.

Thanks,
Maverick


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