GridView Row Coloring based upon condition in ASP.net


GridView Row Coloring based upon Condition in ASP.net..? In this artical i'm going to explain about the color coding of Gridview based upon condition.. Here we have some Drawings , if the Drawing is upload a file then we need to display one color..

Design your Source code based upon your requirement...


Source Code:





<asp:DataGrid ID="gd" runat="server"
AutoGenerateColumns="False" BorderColor="Black"
BorderStyle="Solid" BorderWidth="1px" CellPadding="2"
CssClass="ArialNarrow"GridLines="Vertical" Width="100%"
OnItemDataBound="gd_ItemDataBound" AllowPaging="True"
onpageindexchanged="gd_PageIndexChanged" PageSize="50" >


< PagerStyle HorizontalAlign="center" Mode="NumericPages"
PageButtonCount="10"/>
< SelectedItemStyle BackColor="White"
ForeColor="White" />
< AlternatingItemStyle BackColor="AliceBlue" HorizontalAlign="Left" />
< ItemStyle BackColor="White" ForeColor="Black" HorizontalAlign="Left" VerticalAlign="Top" />
< HeaderStyle HorizontalAlign="Center" VerticalAlign="Top" BackColor="#000084" ForeColor="White" Font-Bold="True" />

< Columns >
< asp:TemplateColumn ItemStyle-Wrap="false" >
< ItemTemplate >
< asp:Label ID="lblNo" runat="server" Text='<%# Eval( "No") %>' />
< asp:Label ID="lblDra" Visible="false" runat="server" Text='<%# Eval("Id") %>' />
< /ItemTemplate >
< /asp:TemplateColumn >

< asp:TemplateColumn >
< ItemTemplate >
< asp:Label ID="lblFun" runat="server" Text='<%# Eval( "Fun") %>' />
</ItemTemplate>
</asp:TemplateColumn>

< asp:TemplateColumn HeaderText="Desc" >
< ItemTemplate >
< asp:Label ID="lblDesc" runat="server" Text='<%# Eval( "Desc") %>' />
</ItemTemplate>
</asp:TemplateColumn>

< asp:TemplateColumn >
< ItemTemplate >
< asp:Label ID="lblApp" runat="server" Text='<%# Eval( "App_Date","{0:dd-MMM-yyyy}") %>' Visible="false" / >
< asp:Label ID="lblRev" runat="server" Text='<%#Eval("Rev_No") %>'/>
< /ItemTemplate >
< /asp:TemplateColumn >

< asp:TemplateColumn >
< ItemTemplate >
< asp:Label ID="lblSze" runat="server" Text='<%# Eval( "Size_Desc") %>'/>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn>
< ItemTemplate >
< asp:LinkButton ID="btnEdit" CommandName="Edit" runat="server" Text="Upload" CausesValidation="false" />
</ItemTemplate>
</asp:TemplateColumn>

</Columns>
</asp:DataGrid>



Code Behind:






using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WebControls;
using System.Diagnostics;
// using this i'm calling dll's
// using this i'm calling my webreference

public partial class Resource1 : System.Web.UI.Page
{
string Web_Login;
double UserId;
string Code;
int C_Code = 0;



protected void Page_Load(object sender, EventArgs e)
{
lntSPCurrentSite objCurrentSite = new lntSPCurrentSite();
lntSPCurrentUser objCurrentUser = new lntSPCurrentUser();
Design objDesign = new Design();

Web_Login = objCurrentUser.LoginName;
UserId = objDesign.UserId_WebLogin(Web_Login);

Code = objCurrentSite.WebSiteName;

if (!IsPostBack)
{
Document();
}
}

//Bind GridView Data
protected void Document()
{
Design objDocument = new Design();
DataSet dsDocument = new DataSet();

dsDocument = //fetch data;
ViewState["dsDocument"] = dsDocument;
gd.DataSource = dsDocument;
gd.DataBind();
}

protected void gd_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//File uploaded date
string App_Date = ((Label)e.Item.FindControl("lblApp")).Text;
string Id = ((Label)e.Item.FindControl("lblDwg")).Text;
string Rev = ((Label)e.Item.FindControl("lblRev")).Text;

DataTable dtDwg = (ViewState["dsDocument"] as DataSet).Tables[0];

//Here i'm checking the Uploaded Dwg's Status

DataRow[] drow = dtDrawing.Select("Id=" + Id + " AND OutCome='000006'");

if (App_Date != "")
{
//Here i'm fetching file uploaded date
DateTime x = Convert.ToDateTime(((Label)e.Item.FindControl("lblApp")).Text);

// Here i'm fecthing System Date.
DateTime y = DateTime.Now.Date;

//taking the difference between 2 dates
int z = Convert.ToInt32((y - x).TotalDays);


//Dwg is upload it's comes to else part
if (drow != null && drow.Length == 0)
{
// difference is in betweem 0 to 15 days then show the color yellow
if (z >= 0 && z < 15)
{
e.Item.BackColor = System.Drawing.Color.LightYellow;

}

//dfference is less than 0, then Gainsboro color.
else if (z < 0)
{

e.Item.BackColor = System.Drawing.Color.Gainsboro;

}

else
e.Item.BackColor = System.Drawing.Color.LightSalmon;
}

// Dwg already uploaded then Green Color
else
{
e.Item.BackColor = System.Drawing.Color.PaleGreen;
}
}
}
}


//Apply paging to the GridView...

protected void gd_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
gd.CurrentPageIndex = e.NewPageIndex;
Document();
}
}


Article by naveensanagasetti
I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: