Populate DataGridView in VB.Net

DataGridView provides a visual interface to data. It is an excellent way to display and allow editing for your data. It is accessed with VB.NET code. Data edited in the DataGridView can then be persisted in the database.

How it works?

In your windows form application, add DataGridView component
Add TextBox control for filtering records. (see the code snippet below)

Screenshot


Code snippet
Sub View_Record(ByVal pSearch As String)
      Try
            Dim sql As String ="SELECT studentno, name, course, mobileno FROM Student " & _
            "where [name] like '" & pSearch & "%' order by [name]"
            Dim da As New SqlDataAdapter(sql, cn)
            Dim ds As New DataSet()

            da.Fill(ds,"Student")
            DataGridView1.DataSource = ds
            DataGridView1.DataMember = "Student"
            DataGridView1.ColumnHeadersBorderStyle = DataGridView1HeaderBorderStyle.Single
            DataGridView1.RowsDefaultCellStyle.BackColor = Color.White
            DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightSteelBlue
            DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
            DataGridView1.Columns(0).HeaderText = "Student No"
            DataGridView1.Columns(1).HeaderText = "Name"
            DataGridView1.Columns(2).HeaderText = "Program"
            DataGridView1.Columns(3).HeaderText = "Mobile"
     Catch ex As Exception

     End Try
End Sub

Programming Language: VB.Net
Programming IDE: Visual Studio 2010
Database MS Sql