Example : How to count the no. of rows and columns in a datagridview.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim rowcount As Integer = DataGridView1.Rows.Count
//Dim rowcount As Integer = DataGridView1.RowCount
TextBox1.Text = DataGridView1.RowCount
-------------------------------------------------
Dim colcount As Integer= DataGridView1.Columns.Count
Dim colcount As Integer= DataGridView1.ColumnCount
End Sub
Example : How to increase width and height of rows and columns in a datagridview.
Private Sub DataSaving_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.Columns(2).Width = 310 'For single column
------------------------------------------------
Dim colcount As Integer= DataGridView1.ColumnCount
For i As Integer = 0 To colcount - 1
DataGridView1.Columns(i).Width = 210
Next
------------------------------------------------
For Each Col As DataGridViewColumn In DataGridView1.Columns
Col.Width = 240
Next
End Sub
Example : How to stretch less no. of columns in full area of datagridview.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
End Sub
Example : How to autofit column size of datagridview according to contents.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.AutoResizeColumns()
-------------------- OR -------------------------
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.AllCells
//DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnMode.AllCells)
-------------------- OR -------------------------
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader
-------------------- OR -------------------------
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.ColumnHeader
-------------------- OR -------------------------
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.DisplayedCells
-------------------- OR -------------------------
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.DisplayedCellsExceptHeader
End Sub
Example : How to remove left most extra column from a datagridview.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.RowHeadersVisible = False
End Sub
Example : How to stop resizing rows/columns manually in a datagridview.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.AllowUserToResizeColumns = False
DataGridView1.AllowUserToResizeRows = False
End Sub
Example : How to fit the text completely in a datagridview cell.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True
DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
End Sub
Example : How to get/find row index of datagridview records.
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As EventArgs) Handles
DataGridView1.DoubleClick
Dim p As String = DataGridView1.Rows(e.RowIndex).Index
End Sub
Example : How to print/display/get specific cell value of datagridview records.
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As EventArgs) Handles
DataGridView1.DoubleClick
Dim p As String = DataGridView1.Rows(e.RowIndex).Cells(5).Value 'Column Index 5 from 0/Column No.6
End Sub
Example : How to clear a datagridview contents.
DataGridView1.DataSource = Nothing 'To clear the contents of DataGridView or
'DataGridView1.Rows.Clear() 'clear datagridview
Example : How to select full row in a datagridview by simply click any cell.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
Example : How to check/verify that datagridview is blank/empty or not.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If DataGridView1.Columns.Count = 0 Then
MsgBox("Datagrid view is empty")
End If
End Sub
'-------------------- OR ---------------------
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If DataGridView1.Rows.Count = 0 Then
MsgBox("Datagrid view is empty")
End If
End Sub
Example : How to add values from VB controls (Text/Combo Box etc.) to Datagridview sequentially.
NB : First of all create required columns in datagridview manually/statically.
Private Sub RtbAddress_KeyDown(sender As Object, e As KeyEventArgs) Handles RtbAddress.KeyDown
Dim gen1 As String
Dim co As String
Dim qm As String
Dim qi As String
Dim x As Integer= 10143
Dim y As String = "Codershelpline"
If e.KeyCode = Keys.Enter Then
If RdbGender1.Checked = True Then
gen1 = "Male"
ElseIf RdbGender2.Checked = True Then
gen1 = "Female"
End If
If ChkMatric.Checked = True Then
qm = "Matric"
End If
If ChkInter.Checked = True Then
qi = "Inter"
End If
DataGridView1.Rows.Add(x.ToString, TxtName.Text, DtpDob.Text, gen1.ToString, qm.ToString
+","+ qi.ToString, NumMobile.Text, CmbNational.Text, TxtUname.Text, TxtPass.Text,
RtbAddress.Text, y.ToString)
End If
End Sub
'-------------------- OR ---------------------
Private Sub RtbAddress_KeyDown(sender As Object, e As KeyEventArgs) Handles RtbAddress.KeyDown
Dim gen1 As String
Dim co As String
Dim qm As String
Dim qi As String
Dim x As Integer= 10143
Dim y As String = "Codershelpline"
If e.KeyCode = Keys.Enter Then
If RdbGender1.Checked = True Then
gen1 = "Male"
ElseIf RdbGender2.Checked = True Then
gen1 = "Female"
End If
If ChkMatric.Checked = True Then
qm = "Matric"
End If
If ChkInter.Checked = True Then
qi = "Inter"
End If
Dim RowId As Integer = DataGridView1.Rows.Add
DataGridView1.Rows(RowId).Cells(0).Value = x.ToString
DataGridView1.Rows(RowId).Cells(1).Value = TxtName.Text
DataGridView1.Rows(RowId).Cells(2).Value = DtpDob.Text
DataGridView1.Rows(RowId).Cells(3).Value = gen1.ToString
DataGridView1.Rows(RowId).Cells(4).Value = qm.ToString + "," + qi.ToString
DataGridView1.Rows(RowId).Cells(5).Value = NumMobile.Text
DataGridView1.Rows(RowId).Cells(6).Value = CmbNational.Text
DataGridView1.Rows(RowId).Cells(7).Value = TxtUname.Text
DataGridView1.Rows(RowId).Cells(8).Value = TxtPass.Text
DataGridView1.Rows(RowId).Cells(9).Value = RtbAddress.Text
DataGridView1.Rows(RowId).Cells(9).Value = y.ToString
End If
End Sub
Example : How to add values into respective vb controls/boxes on DoubleClick event in data grid row/cell contents.
Private Sub DataGridView1_DoubleClick(sender As Object, e As EventArgs) Handles DataGridView1.DoubleClick
Dim gen1 As String
Dim co As String
Dim qm As String
Dim qi As String
Dim y As String = "Codershelpline"
Dim RowId As Integer = DataGridView1.SelectedCells(0).RowIndex
TxtName.Text = DataGridView1.Rows(RowId).Cells(1).Value.ToString
DtpDob.Text = DataGridView1.Rows(RowId).Cells(2).Value.ToString
gen1 = DataGridView1.Rows(RowId).Cells(3).Value.ToString
If gen1.Trim().ToString = "Male" Then
RdbGender1.Checked = True
ElseIf gen1.Trim().ToString = "Female" Then
RdbGender2.Checked = True
End If
qm = DataGridView1.Rows(RowId).Cells(4).Value.ToString
If qm.Trim().ToString = "Matric" Then
ChkMatric.Checked = True
End If
qi = DataGridView1.Rows(RowId).Cells(5).Value.ToString
If qi.Trim().ToString = "Inter" Then
ChkInter.Checked = True
End If
NumMobile.Text = DataGridView1.Rows(RowId).Cells(6).Value.ToString
co = DataGridView1.Rows(RowId).Cells(7).Value.ToString
Me.CmbNational.SelectedItem = co.Trim()
TxtUname.Text = DataGridView1.Rows(RowId).Cells(8).Value.ToString
TxtPass.Text = DataGridView1.Rows(RowId).Cells(9).Value.ToString
RtbAddress.Text = DataGridView1.Rows(RowId).Cells(10).Value.ToString
TxtRemarks.Text = y.ToString
End Sub
Example : How to add values into respective vb controls/boxes on CellDoubleclick event in datagrid row/cell contents.
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
Dim gen1 As String
Dim co As String
Dim qm As String
Dim qi As String
Dim y As String = "Codershelpline"
TxtName.Text = DataGridView1.Rows(e.RowIndex).Cells(1).Value.ToString
DtDob.Text = DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString
gen1 = DataGridView1.Rows(e.RowIndex).Cells(3).Value.ToString
If gen1.Trim().ToString = "Male" Then
RdbGender1.Checked = True
ElseIf gen1.Trim().ToString = "Female" Then
RdbGender2.Checked = True
End If
qm = DataGridView1.Rows(e.RowIndex).Cells(4).Value.ToString
If qm.Trim().ToString = "Matric" Then
ChkMatric.Checked = True
End If
qi = DataGridView1.Rows(e.RowIndex).Cells(5).Value.ToString
If qi.Trim().ToString = "Inter" Then
ChkInter.Checked = True
End If
NumMobile.Text = DataGridView1.Rows(e.RowIndex).Cells(6).Value.ToString
co = DataGridView1.Rows(e.RowIndex).Cells(7).Value.ToString
Me.CmbNational.SelectedItem = co.Trim()
TxtUname.Text = DataGridView1.Rows(e.RowIndex).Cells(8).Value.ToString
TxtPass.Text = DataGridView1.Rows(e.RowIndex).Cells(9).Value.ToString
RtbAddress.Text = DataGridView1.Rows(e.RowIndex).Cells(10).Value.ToString
TxtRemarks.Text = y.ToString
Example : How to display same database fields name & its respective data in datagridview header automatically.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Draw a datagridview only not columns
'Put sql server database connectivity code here first.
ds = New DataSet()
da = New SqlClient.SqlDataAdapter("select AdmSlno5,AdmName5,AdmMname5,AdmFname5,
AdmDob5,AdmMobile5,AdmRemarks5 from UserRegdTable1 Order By AdmSlno5", cs)
'da = New SqlClient.SqlDataAdapter("select * from UserRegdTable1 Order By AdmSlno5", cs)
da.Fill(ds, "UserRegdTable1")
bnd = New BindingSource()
bnd.DataMember = "UserRegdTable1"
bnd.DataSource = ds
DataGridView1.DataSource = bnd
DataGridView1.Refresh()
End Sub
Example : How to add customized header name dynamically (through sql) along with database data in respective box of datagridview automatically.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Put sql server database connectivity code here first.
ds = New DataSet()
da = New SqlClient.SqlDataAdapter("select AdmSlno5 as [Registration No.], AdmName5 as
[Candidate's Name], AdmMname5 as [Mother's Name], AdmFname5 as [Father's Name],
AdmDob5 as [Date of Birth], AdmMobile5 as [Mobile No.], AdmRemarks5 as [Remarks]
from UserRegdTable1 Order By AdmSlno5", cs)
da.Fill(ds, "UserRegdTable1")
bnd = New BindingSource()
bnd.DataMember = "UserRegdTable1"
bnd.DataSource = ds
DataGridView1.DataSource = bnd
DataGridView1.Refresh()
End Sub
Example : How to add customized header name manually along with database data in datagridview automatically with users created own serial number column.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
// create a datagrideview with appropriate header name manually.
// Put sql server database connectivity code here.
DataGridView1.Rows.Clear() ' clear the datagrid content
Dim sn as Integer = 0
cn.Open()
cm.CommandText = "select * from tablename1 order by AdmSlno5"
rdr = cm.ExecuteReader
While (rdr.Read())
sn = sn + 1
Dim RowId As Integer = DataGridView1.Rows.Add
DataGridView1.Rows.Item(RowId).Cells(0).Value = sn
DataGridView1.Rows.Item(RowId).Cells(1).Value = rdr("TxtAdmSlno5").ToString
DataGridView1.Rows.Item(RowId).Cells(2).Value = rdr("TxtAdmName5").ToString & vbNewLine &
rdr("TxtAdmMname5").ToString
DataGridView1.Rows.Item(RowId).Cells(3).Value = rdr("DtAdmDob5").ToString
End While
cn.Close()
End Sub
Example : How to display selected no. of rows/records in a datagridview (having static created headers name) using combo box list .
Dim sn as Integer 'Global declaration
------------------------------------------------
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
// create a datagrideview with appropriate header name
// Put sql server database connectivity code here.
End Sub
------------------------------------------------
Private Sub Combox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles
Combox1.SelectedIndexChanged
DataGridView1.Rows.Clear()
sn= 0
Dim x As String = Combobox1.Text 'Combobox1 contains all,5,10,20--- values.
cn.Open()
cm.CommandText = "select * from tablename1 order by TxtAdmSlno5"
rdr = cm.ExecuteReader
While (rdr.Read())
sn= sn+ 1
If (x = "All") Then
datagridlist()
ElseIf (sn<= Val(x)) Then
datagridlist()
Else
Exit While
End If
End While
cn.Close()
End Sub
------------------------------------------------
Sub datagridlist()
Dim RowId As Integer = DataGridView1.Rows.Add
DataGridView1.Rows.Item(RowId).Cells(0).Value = sn
DataGridView1.Rows.Item(RowId).Cells(1).Value = rdr("TxtAdmSlno5").ToString
DataGridView1.Rows.Item(RowId).Cells(2).Value = rdr("TxtAdmName5").ToString & vbNewLine &
rdr("TxtAdmMname5").ToString
DataGridView1.Rows.Item(RowId).Cells(3).Value = rdr("DtAdmDob5").ToString
End Sub
0 Comments