DataGirdView Row Color And Alternate Row Color Change in Window Application C#

DataGirdView Row Color & Alternate Row Color Change

Change  DataGridView  Row  Color  & Alternate Row Color  in Code Behind   Window Application C#.


                                DEMO



          
                             C# CODING


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication2
{
    public partial class Home11 : Form
    {
        public Home11()
        {
          
            InitializeComponent();
        }

        private void Home11_Load(object sender, EventArgs e)
        {
            gridcolor();
        }
        protected void gridcolor()
        {

      this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.White;
 this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Red;

        }

        private void button1_Click(object sender, EventArgs e)
        {           

   SqlConnection   con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\drizzle\Documents\Visual Studio 2012\WindowsFormsApplication2\WindowsFormsApplication2\Database1.mdf;Integrated Security=True");
            con.Open();

            SqlCommand cmd = new SqlCommand("select * from country",con);
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            SqlDataReader rd = cmd.ExecuteReader();
            if (rd.Read())
            {
                dataGridView1.DataSource = dt;
            }
                  
        }
    }
}

First Add New Window Form - Select - Button & DataGridView




Next - DataBase Values Bind To DataGridview & Set DataGridView Row  & Alternate Row Color & Call To Page_Load






Next - Run[F5] - Click Search Button  - Show DataGridView Row & Alternate Row Colors











0 comments:

Post a Comment