Programmatically change Row Style in DataGridView in Windows Application using C# and VB.Net

Last Reply 4 months ago By pandeyism

Posted 4 months ago

I have more than 20 datagridview in my project. I am trying to change the font of all datagridview. In some datagridview, font of header and alternative lines i.e. 1,3,5..... didn't change. I am trying to change programmatically rather than properties. it changed. But when I am closing application and opening it again, old font remains. I couldn't able to know why this is happend. Help me ...

        Me.productsDGV.AllowUserToAddRows = False
        Me.productsDGV.AllowUserToOrderColumns = True
        DataGridViewCellStyle22.BackColor = System.Drawing.Color.FromArgb(CType(CType(247, Byte), Integer), CType(CType(247, Byte), Integer), CType(CType(247, Byte), Integer))
        DataGridViewCellStyle22.Font = New System.Drawing.Font("Segoe UI Symbol", 10.75!)
        DataGridViewCellStyle22.ForeColor = System.Drawing.Color.FromArgb(CType(CType(50, Byte), Integer), CType(CType(50, Byte), Integer), CType(CType(50, Byte), Integer))
        DataGridViewCellStyle22.SelectionBackColor = System.Drawing.Color.FromArgb(CType(CType(240, Byte), Integer), CType(CType(240, Byte), Integer), CType(CType(240, Byte), Integer))
        DataGridViewCellStyle22.SelectionForeColor = System.Drawing.Color.FromArgb(CType(CType(50, Byte), Integer), CType(CType(50, Byte), Integer), CType(CType(50, Byte), Integer))
        Me.productsDGV.AlternatingRowsDefaultCellStyle = DataGridViewCellStyle22
        Me.productsDGV.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill
        Me.productsDGV.BackgroundColor = System.Drawing.Color.White
        Me.productsDGV.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.productsDGV.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.SingleHorizontal
        Me.productsDGV.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None
        DataGridViewCellStyle23.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft
        DataGridViewCellStyle23.BackColor = System.Drawing.Color.FromArgb(CType(CType(100, Byte), Integer), CType(CType(100, Byte), Integer), CType(CType(100, Byte), Integer))
        DataGridViewCellStyle23.Font = New System.Drawing.Font("Segoe UI Light", 12.0!)
        DataGridViewCellStyle23.ForeColor = System.Drawing.Color.White
        DataGridViewCellStyle23.SelectionBackColor = System.Drawing.Color.FromArgb(CType(CType(100, Byte), Integer), CType(CType(100, Byte), Integer), CType(CType(100, Byte), Integer))
        DataGridViewCellStyle23.SelectionForeColor = System.Drawing.SystemColors.HighlightText
        DataGridViewCellStyle23.WrapMode = System.Windows.Forms.DataGridViewTriState.[True]
        Me.productsDGV.ColumnHeadersDefaultCellStyle = DataGridViewCellStyle23
        Me.productsDGV.ColumnHeadersHeight = 35
        DataGridViewCellStyle24.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft
        DataGridViewCellStyle24.BackColor = System.Drawing.Color.White
        DataGridViewCellStyle24.Font = New System.Drawing.Font("Segoe UI Light", 12.0!)
        DataGridViewCellStyle24.ForeColor = System.Drawing.Color.FromArgb(CType(CType(50, Byte), Integer), CType(CType(50, Byte), Integer), CType(CType(50, Byte), Integer))
        DataGridViewCellStyle24.SelectionBackColor = System.Drawing.Color.FromArgb(CType(CType(240, Byte), Integer), CType(CType(240, Byte), Integer), CType(CType(240, Byte), Integer))
        DataGridViewCellStyle24.SelectionForeColor = System.Drawing.Color.FromArgb(CType(CType(50, Byte), Integer), CType(CType(50, Byte), Integer), CType(CType(50, Byte), Integer))
        DataGridViewCellStyle24.WrapMode = System.Windows.Forms.DataGridViewTriState.[False]
        Me.productsDGV.DefaultCellStyle = DataGridViewCellStyle24
        Me.productsDGV.EnableHeadersVisualStyles = False
        Me.productsDGV.Location = New System.Drawing.Point(25, 110)
        Me.productsDGV.Name = "productsDGV"
        Me.productsDGV.ReadOnly = True
        Me.productsDGV.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None
        Me.productsDGV.RowHeadersVisible = False
        Me.productsDGV.RowHeadersWidth = 35
        Me.productsDGV.RowTemplate.Height = 35
        Me.productsDGV.Size = New System.Drawing.Size(1213, 350)
        Me.productsDGV.TabIndex = 1

 

Posted 4 months ago Modified on 4 months ago

Hi PSowmiya,

Please refer below sample.

Namespaces

C#

using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

VB.Net

Imports System.Data
Imports System.Data.SqlClient

Code

C#

public Form1()
{
    InitializeComponent();
    BindGrid();
}

private void BindGrid()
{
    string constring = @"Data Source=.\SQL2005;Initial Catalog=Northwind;User id = sa;password=pass";
    using (SqlConnection con = new SqlConnection(constring))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT ContactName, City, Country FROM Customers", con))
        {
            cmd.CommandType = CommandType.Text;
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    dataGridView1.DataSource = dt;
                    dataGridView2.DataSource = dt;
                }
            }
        }
    }
}

private void Form1_Load(object sender, System.EventArgs e)
{
    this.dataGridView1.Font = new Font("Verdana", 12);
    this.dataGridView1.BackgroundColor = System.Drawing.Color.White;
    this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
    this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.White;
    this.dataGridView1.AlternatingRowsDefaultCellStyle.Font = new Font("Arial", 15);
    this.dataGridView1.RowTemplate.Height = 35;
    this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Navy;
    this.dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Red;

    this.dataGridView2.Font = new Font("Segoe UI Light", 12);
    this.dataGridView2.BackgroundColor = System.Drawing.Color.White;
    this.dataGridView2.RowsDefaultCellStyle.BackColor = Color.Bisque;
    this.dataGridView2.AlternatingRowsDefaultCellStyle.BackColor = Color.Red;
    this.dataGridView2.AlternatingRowsDefaultCellStyle.Font = new Font("Segoe UI Light", 15);
    this.dataGridView2.RowTemplate.Height = 35;
    this.dataGridView2.DefaultCellStyle.SelectionBackColor = Color.Navy;
    this.dataGridView2.DefaultCellStyle.SelectionForeColor = Color.Red;
}

VB.Net

Public Sub New()
    InitializeComponent()
    BindGrid()
End Sub

Private Sub BindGrid()
    Dim constring As String = "Data Source=.\SQL2005;Initial Catalog=Northwind;User id = sa;password=pass"
    Using con As New SqlConnection(constring)
        Using cmd As New SqlCommand("SELECT ContactName, City, Country FROM Customers", con)
            cmd.CommandType = CommandType.Text
            Using sda As New SqlDataAdapter(cmd)
                Using dt As New DataTable()
                    sda.Fill(dt)
                    dataGridView1.DataSource = dt
                    dataGridView2.DataSource = dt
                End Using
            End Using
        End Using
    End Using
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Me.dataGridView1.Font = New Font("Verdana", 12)
    Me.dataGridView1.BackgroundColor = System.Drawing.Color.White
    Me.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque
    Me.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.White
    Me.dataGridView1.AlternatingRowsDefaultCellStyle.Font = New Font("Arial", 15)
    Me.dataGridView1.RowTemplate.Height = 35
    Me.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Navy
    Me.dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Red
    Me.dataGridView2.Font = New Font("Segoe UI Light", 12)
    Me.dataGridView2.BackgroundColor = System.Drawing.Color.White
    Me.dataGridView2.RowsDefaultCellStyle.BackColor = Color.Bisque
    Me.dataGridView2.AlternatingRowsDefaultCellStyle.BackColor = Color.Red
    Me.dataGridView2.AlternatingRowsDefaultCellStyle.Font = New Font("Segoe UI Light", 15)
    Me.dataGridView2.RowTemplate.Height = 35
    Me.dataGridView2.DefaultCellStyle.SelectionBackColor = Color.Navy
    Me.dataGridView2.DefaultCellStyle.SelectionForeColor = Color.Red
End Sub

Screenshot