This C# program change the alternate rows color in DataGridview control in C#. Create New Project and give the Name of the project is RowcolorChangeindatagridview and Add the DataGrid View Control then copy this code and paste.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceRowcolorChangeindatagridview
{
public partial class Form1 : Form
{
publicForm1()
{
InitializeComponent();
}
privatevoid Form1_Load_1(objectsender, EventArgs e)
{
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = “Emp_code”;
dataGridView1.Columns[1].Name = “Emp_Name”;
dataGridView1.Columns[2].Name = “Emp_Salary”;
//Adding Row in DataGridview
string[] rw = new string[] { “101”, “John”, “15000” };
dataGridView1.Rows.Add(rw);
string[] rw1 = new string[] { “102”, “Fleming”, “10000” };
dataGridView1.Rows.Add(rw1);
string[] rw2 = new string[] { “103”, “Steph”, “60000” };
dataGridView1.Rows.Add(rw2);
string[] rw3 = new string[] { “104”, “Jay”, “50000” };
dataGridView1.Rows.Add(rw3);
dataGridView1.AlternatingRowsDefaultCellStyle.ForeColor = System.Drawing.Color.Black ;
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.Blue;
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.White;
}
}