dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersbaskar
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » ASP.NET WebForms

How to give alternate color based on record count?


Posted Date:     Category: ASP.NET WebForms    
Author: Member Level: Diamond    Points: 75


In this article I am going to explain about how to give alternate color through code behind based on record count.



 


Description


Here I have lots of record in the grid view I want to change only back ground of each two records. So not able to assign alternate in design view for each two records. In that time set row back color using code behind code, in this article I am explain in that concept with ASP.NET code and C#.NET windows application code.

ASP.NET Example


Client Side

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter Eno<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
Enter Empname<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
Enter Sal<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" /><br />
<asp:GridView ID="GridView1" runat="server" DataKeyNames="eno" AutoGenerateColumns="false"
onrowediting="GridView1_RowEditing"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting"
onpageindexchanging="GridView1_PageIndexChanging" PageSize="6"
AllowPaging="true" onrowcreated="GridView1_RowCreated"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Employee no">
<ItemTemplate>
<%#Eval("eno")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emp name">
<ItemTemplate>
<%#Eval("empname")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<ItemTemplate>
<%#Eval("sal")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" CommandName="Delete" runat="server" OnClientClick="return confirm('Are you sure you want to delete this record?');"
>Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

Code Behind

using System.Data;
public partial class _Default : System.Web.UI.Page
{
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
DataRow dr;

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dt.Columns.Add("eno");
dt.Columns.Add("empname");
dt.Columns.Add("sal");
Session["reptable"] = dt;
GridData();
}
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridData();
}

void GridData()
{
GridView1.DataSource = (DataTable)Session["reptable"];
GridView1.DataBind();
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridData();
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string eno;
eno = GridView1.DataKeys[e.RowIndex].Value.ToString();
if (Session["reptable"] != null)
{
DataTable dt1 = new DataTable();
dt1.Clear();
dt1 = Session["reptable"] as DataTable;
for (int i = 0; i <= dt1.Rows.Count - 1; i++)
{
DataRow dr;
if (dt1.Rows[i][0].ToString() == eno)
{
dr = dt1.Rows[i];
dt1.Rows[i].Delete();
//dt1.Rows.Remove(dr);
}
}
Session.Remove("reptable");
Session["reptable"] = dt1;
}
GridData();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridData();
}
protected void Button1_Click(object sender, EventArgs e)
{
dt = (DataTable)Session["reptable"];
dr = dt.NewRow();
dr["eno"] = TextBox1.Text;
dr["empname"] = TextBox2.Text;
dr["sal"] = TextBox3.Text;
dt.Rows.Add(dr);
Session.Remove("reptable");
Session["reptable"] = dt;
GridData();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

}
//here i cah change back color each two records
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{
GridViewRow row = e.Row;
if (e.Row.RowIndex < 2)
{
row.BackColor = System.Drawing.Color.LightBlue;
}
else if (e.Row.RowIndex < 4)
{
row.BackColor = System.Drawing.Color.Pink;
}
else if (e.Row.RowIndex < 6)
{
row.BackColor = System.Drawing.Color.Aqua;
}
}
}
}


Output


GridView_RowColor

C#.NET Example



namespace DataGridViewColorChange
{
public partial class Form1 : Form
{
SqlConnection sqlcon = new SqlConnection(@"Server=RAVI-PC\SQLEXPRESS;database=test1;uid=ravindran;pwd=srirangam;");
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
LoadGrid();
//Change back color using below method after bind data
colorchange();
}

void colorchange()
{
if (dt.Rows.Count > 2)
{
for (int i = 0; i < 2; i++)
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Aqua;
}
}
else
{
for (int i = 0; i <= dt.Rows.count-1; i++)
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Aqua;
}
}

if (dt.Rows.Count > 4)
{
for (int i = 2; i < 4; i++)
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Pink;
}
}
else
{
for (int i = 2; i <= dt.Rows.count - 1; i++)
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Pink;
}
}
}
}
}

Output


dgv_rowclor

Source code:

Client Side: ASP.NET
Code Behind: C#

Conclusion

I hope this article is help you to know change back color of gridview and datagridview through code behind based on the record .

Attachments
  • GridRowColor_Change_RecordBased (44062-11228-GridRowColor-Change-RecordBased.rar)





  • Did you like this resource? Share it with your friends and show your love!


    Responses to "How to give alternate color based on record count?"

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

    Feedbacks      

    Post 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to use Details view control in ASP.NET?
    Previous Resource: Printing aspx page wtithout viewing the content
    Return to Resources
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    GridView Row Color  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.