ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Programmatically change Row Style in DataGridView in Windows Application using C# and VB.Net<p>Hi <a class="username" rel="PSowmiya">PSowmiya</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Drawing; using System.Windows.Forms; using System.Data; using System.Data.SqlClient;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Data.SqlClient</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">public Form1() { InitializeComponent(); BindGrid(); } private void BindGrid() { string constring = @&#34;Data Source=.\SQL2005;Initial Catalog=Northwind;User id = sa;password=pass&#34;; using (SqlConnection con = new SqlConnection(constring)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT ContactName, City, Country FROM Customers&#34;, 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(&#34;Verdana&#34;, 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(&#34;Arial&#34;, 15); this.dataGridView1.RowTemplate.Height = 35; this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Navy; this.dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Red; this.dataGridView2.Font = new Font(&#34;Segoe UI Light&#34;, 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(&#34;Segoe UI Light&#34;, 15); this.dataGridView2.RowTemplate.Height = 35; this.dataGridView2.DefaultCellStyle.SelectionBackColor = Color.Navy; this.dataGridView2.DefaultCellStyle.SelectionForeColor = Color.Red; }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Public Sub New() InitializeComponent() BindGrid() End Sub Private Sub BindGrid() Dim constring As String = &#34;Data Source=.\SQL2005;Initial Catalog=Northwind;User id = sa;password=pass&#34; Using con As New SqlConnection(constring) Using cmd As New SqlCommand(&#34;SELECT ContactName, City, Country FROM Customers&#34;, 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(&#34;Verdana&#34;, 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(&#34;Arial&#34;, 15) Me.dataGridView1.RowTemplate.Height = 35 Me.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Navy Me.dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Red Me.dataGridView2.Font = New Font(&#34;Segoe UI Light&#34;, 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(&#34;Segoe UI Light&#34;, 15) Me.dataGridView2.RowTemplate.Height = 35 Me.dataGridView2.DefaultCellStyle.SelectionBackColor = Color.Navy Me.dataGridView2.DefaultCellStyle.SelectionForeColor = Color.Red End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/lFRLzXU.jpg" alt="" width="472" height="158" /></p> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/212615/Programmatically-change-Row-Style-in-DataGridView-in-Windows-Application-using-C-and-VBNet/https://www.aspforums.net:443/Threads/212615/Programmatically-change-Row-Style-in-DataGridView-in-Windows-Application-using-C-and-VBNet/Mon, 29 Oct 2018 07:52:54 GMT