How to Populate Show and Hide Data in Datagridview in VB.net

Heres another tutorial for beginners on how to populate your data to a datagrid and other stuff to manage your records and maximize the capability of your datagridview


First, create a new project and add 7 Buttons and 1 Datagridview, refer to the image below


Button
Name Properties                                Text Properties
btnShowRow                                       Show Row
btnHideRow                                        Hide Row
btnShowCol                                         Show Column
btnHideCol                                          Hide Column
btnDelete                                            Delete Row
btnClear                                              Clear
btnPopulate                                         Populate Row

Apply the code

Imports System.Windows.Forms

Public Class Form2

    Dim rowIndex As Integer = 0
    Dim colIndex As Integer = 0
    Dim f As Object

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnDelete.Enabled = False
        btnHideCol.Enabled = False
        btnHideRow.Enabled = False
        btnShowCol.Enabled = False
        btnShowRow.Enabled = False
        btnClear.Enabled = False
    End Sub

‘Populating the Datagridview
    Private Sub btnPopulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPopulate.Click
        btnPopulate.Enabled = False
        btnDelete.Enabled = True
        btnHideCol.Enabled = True
        btnHideRow.Enabled = True
        btnShowCol.Enabled = True
        btnShowRow.Enabled = True
        btnClear.Enabled = True

        DataGridView1.Rows.Clear()
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect

        DataGridView1.ColumnCount = 3
        DataGridView1.Columns(0).Name = "Name"
        DataGridView1.Columns(1).Name = "Position"
        DataGridView1.Columns(2).Name = "Team"

      ‘In this area we assume that we already have a data to be populate in the datagrid
        Dim row As String() = New String() {"Phil Jones5", "Defender", "Man Utd"}
        DataGridView1.Rows.Add(row)

        row = New String() {"Phil Jones1", "Defender", "Man Utd"}
        DataGridView1.Rows.Add(row)

        row = New String() {"Phil Jones2", "Defender", "Man Utd"}
        DataGridView1.Rows.Add(row)

        row = New String() {"Phil Jones3", "Defender", "Man Utd"}
        DataGridView1.Rows.Add(row)

        row = New String() {"Phil Jones4", "Defender", "Man Utd"}
        DataGridView1.Rows.Add(row)

      ‘In this area you need change pictures or image directory
        Dim pic As DataGridViewImageColumn = New DataGridViewImageColumn()
        Dim img As Image = Image.FromFile("C:/Users/Desktop/pics/billgates.jpg")
        pic.HeaderText = "Picture"
        pic.Name = "pic"
        pic.Image = img
        DataGridView1.Columns.Add(pic)
    End Sub

‘This code is when you click the cell content of your datagrid
      Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Me.rowIndex = e.RowIndex
    End Sub

‘Show row button
    Private Sub btnShowRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowRow.Click
        DataGridView1.Rows(rowIndex).Visible = True
    End Sub

‘Hide row button
    Private Sub btnHideRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHideRow.Click
        DataGridView1.Rows(rowIndex).Visible = False
    End Sub

‘Show column button
    Private Sub btnShowCol_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowCol.Click
        DataGridView1.Columns(colIndex).Visible = True
    End Sub

‘Hide column button
    Private Sub btnHideCol_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHideCol.Click
        DataGridView1.Columns(colIndex).Visible = False
    End Sub

‘Delete row button
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If (MessageBox.Show("Are you sure to delete?", "DELETE", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)) = Windows.Forms.DialogResult.Yes Then
            DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(0).Index)
        End If
    End Sub

‘Clear the datagridview and buttons
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        btnPopulate.Enabled = True
        btnDelete.Enabled = False
        btnHideCol.Enabled = False
        btnHideRow.Enabled = False
        btnShowCol.Enabled = False
        btnShowRow.Enabled = False
        btnClear.Enabled = False
        DataGridView1.Rows.Clear()
    End Sub
End Class


FINAL OUTPUT


How to Populate Show and Hide Data in Datagridview in VB.net How to Populate Show and Hide Data in Datagridview in VB.net Reviewed by code-dev on 2:19 PM Rating: 5

No comments:

Powered by Blogger.