You are currently viewing How to format DataGridView In VB.Net Programmatically?
how to format datagridview in

How to format DataGridView In VB.Net Programmatically?

How To Format DataGridView

DataGridView is a essential part in developing any softare in .Net Framework. Datagridview is use to retriew the data from the database like MySql, Sql Server, MS Access and much more.

So in this article will help you to retriew the data from databasse and you can format it according to your desired with custom class.

Step 01.     Add a Module in and past the following code.

Imports System.Data.OleDb

Module Modulecon2
Public Sub DataGridProperty(ByVal dgvLog As DataGridView)
With dgvLog
.ReadOnly = True
.MultiSelect = False
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.ShowRowErrors = False
.ShowCellErrors = False
.AllowUserToAddRows = False
.AllowUserToResizeColumns = False
.AllowUserToResizeRows = False

.DefaultCellStyle.SelectionBackColor = Color.DarkSlateBlue
.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray
End With
End Sub

Step 02.     Call the above function in Form Load event as below.

how to format datagridview in

You need to one more step to complete your DataGridView  formatting for this open your VS.Net and click on DataGridView control and change the following properties as below in pics.

how to format datagridview in

How To Load Table Data In DataGridView From Database

It is very easy to load data table in DataGridView to view the table record and sort it as you with. There are  two way to load data in DataGridView and these method are follow.

Example 01 :

Private Sub dgvrefresh()
Using conn As New OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|e_clinic.mdb;”)
Dim command As New OleDbCommand(“SELECT * from patient”, conn)
Dim adapter As New OleDbDataAdapter
Dim dt As New DataTable
adapter.SelectCommand = command
dgw.DataSource = dt
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, “ERROR3”, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Example 02 :

Dim dano As New OleDb.OleDbDataAdapter(“select * FROM [test_rpt] WHERE [Transid] Like ‘%%” & txtsearch.Text & “%%’ or [patient_name] Like ‘%%” & txtsearch.Text & “%%’ or [mobileno] Like ‘%%” & txtsearch.Text & “%%'”, con)
Dim dsno As New DataSet
dano.Fill(dsno, “test_rpt”)
dgw.DataSource = dsno.Tables(“test_rpt”).DefaultView


Format DataGridView Column Programmatically

To format the DataGridView you need to create function and write the following code.

Public Sub addDGV()
With dgw
.Columns(0).Width = 80
.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

.Columns(1).Width = 200
.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
.Columns(2).Width = 150
.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
.Columns(3).Width = 100
.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
.Columns(4).Width = 100
.Columns(6).Width = 150
.Columns(7).Width = 120
End With
End Sub