Wednesday, November 16, 2016

Post #6 : Programming "How to add rows DataGridView manually using VB.Net"

Hi Jack.

We will try to fill or to add row DataGridView manually.
the steps are.


1. Create DataGridView Control, you can found DataGridControl in tab data.



Put DataGridView Control into a form that we provide.



2. Create a columns that we want to show, click on DataGridView, in the properties windows find columns.




to add columns clik add button and new popup window will appear.



fill column name and column header text like this.



Continue creating columns, once finished it will look like this.


3. Last but the mots important is make the script.




Imports System.Data.SqlClient
Public Class view_car_data_grid_view
    Private Sub view_car_data_grid_view_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        fill_datagridview()
    End Sub

    Private Function get_data() As DataSet
        Dim dataset As New DataSet
        Using iKon = New SqlConnection(koneksi_server.koneksi)
            If iKon.State = ConnectionState.Open Then iKon.Close()
            iKon.Open()
            Using iCmd = New SqlCommand("select * from cars", iKon)
                Using adapter = New SqlDataAdapter
                    adapter.SelectCommand = iCmd
                    adapter.Fill(dataset)
                End Using
            End Using
        End Using
        Return dataset
    End Function

    Private Sub fill_datagridview()
        Dim dataset As New DataSet
        DataGridView1.Rows.Clear()
        dataset = get_data()
        Dim data As String()
        For Each row As DataRow In dataset.Tables(0).Rows
            data = New String() {row.Item(0), row.Item(1), row.Item(2), row.Item(3)}
            DataGridView1.Rows.Add(data)
        Next
        DataGridView1.Refresh()
    End Sub
End Class

CONGRATULATIONS !!!

Sample Project : Download Here

No comments:

Post a Comment