Menus

Sunday, 26 November 2017

VB.NET DataGridView Tutorials





Adding checkbox column to DataGridView


  Dim AddColumn As New DataGridViewCheckBoxColumn

  With AddColumn
    .HeaderText = "ColumnName"
    .Name = "Column Name that will be displayed"
    .Width = 80
  End With

  dgAdmin.Columns.Insert(1, AddColumn)


datagridview column header checkbox


Public Class search


Private col As DataGridViewCheckBoxColumn

Private chkBox As CheckBox


Private Sub search_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load



col = New DataGridViewCheckBoxColumn()
col.Name = "select"
col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
dgcourse.Columns.Insert(0, col)

chkBox = New CheckBox()
Dim rect As Rectangle = dgcourse.GetCellDisplayRectangle(0, -1, True)
chkBox.Size = New Size(18, 18)
chkBox.Location = rect.Location
AddHandler chkBox.CheckedChanged, AddressOf chkBox_CheckedChanged

dgcourse.Controls.Add(chkBox)



End Sub


Private Sub chkBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
'checkbox value change 
For i As Integer = 0 To dgcourse.Rows.Count - 1
dgcourse(0, i).Value = chkBox.Checked
Next


End Sub



End Class



DataGridView - how to hide the “new” row?



Click on datagridview and change properties AllowUserToAddRows set as false.


Programmatically,
myGrid.AllowUserToAddRows = False


Read the checked rows in a datagridview


For i = 0 To dgcourse.Rows.Count - 1

   If dgcourse.Rows(i).Cells("select").Value Then
      'dgcourse.Item(1, i).Value 
    End If


Next i


Delete row in datagridview

DataGridView1.Rows.Remove(DataGridView1.Rows(x))

Make one column set as editable

Datagridview Readonly Property set as True - all columns are not editable.


dataGridView1.Columns("ColumnName").ReadOnly = false;