Subscribe to Subscribers
Talk to Webmaster Tony John


Forums » .NET » .NET »

How to give alternate color for Evey 100 records in Data Grid view control in C#


Posted Date: 20 Jul 2012      Posted By:: Kumar     Member Level: Silver    Member Rank: 2828     Points: 1   Responses: 1



How to give alternate color for Evey 100(its dynamic may be 100 or 200 or 1, 50, some thing) records in Data Grid view control in C#

Using Windows application




Responses

#681278    Author: Ravindran        Member Level: Diamond      Member Rank: 3     Date: 20/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

Using common method you can change back color of row after bind data refer below code sample


namespace DataGridViewOperation
{
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 LoadGrid()
{
sqlcon.Open();
sqlcmd = new SqlCommand("select * from emp", sqlcon);
da = new SqlDataAdapter(sqlcmd);
da.Fill(dt);
bind = true;
dataGridView1.DataSource = dt;
sqlcon.Close();
}
void colorchange()
{
for(int i =0;i<=99;i++)
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Aqua;
}
for(int i =100;i<=199;i++)
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Blue;
}
//Make sure split rows based on row count if row is not in that count may be index out of range exception come.
}
}
}


Regards
N.Ravindran
Your Hard work never fails



 
Post Reply
You must Sign In to post a response.

Next : How to send two gridviews data to excel
Previous : School management system
Return to Discussion Forum
Post New Message
Category:

Related Messages



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

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
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.