Welcome to ADO.NET Access 2003—your ultimate hub for VB.NET and ADO.NET programming excellence. Discover in-depth tutorials, practical code samples, and expert troubleshooting guides covering a broad range of topics—from building robust WinForms applications and seamless MS Access integration to working with SQL Server, MySQL, and advanced tools like WebView2 and Crystal Reports. Whether you're a beginner or a seasoned developer, our step-by-step articles are designed to empower you to optimize.

Looking for MS Access Developer❓❓

Application developer

Post Page Advertisement [Top]

VB .NET CRUD

Create, Read, Update, Delete

DATAGRIDVIEW with SQL Server

VB.NET DataGridView CRUD SQL Server

Note

If you don't know how to work SQL Server With VB .NET : You need to see this then this
Logic
Using DataGridView Control In VB .NET to Add New - Edit - Delete from/to SQL Server Database or MS-Access or any other Database type. You will find in the end of the lessons the project to download along with source code . 

Design

  1. Create a new Visual Basic project (Windows Application) name it as (MyDg1) and save it to your hard drive.
  2. Place a DataGridView Control on the Form1.
  3. Create SQL Server Database file, name it (MyDB)
  4. Create Table, name it (Kinds) with just one record, name it (Kinds)
  5. Rename the DataGridView Control's name From (DataGridView1) to (DG1)
  6. Save/Build the project .
  7. Now you have: A project such as this one in the 3 photos (from inside VB .NET and from Outside)
Visual Basic Online Courses
vb.net DataGridView Project From Inside VB.Net 2005


Visual Basic Courses Online SQL Server CRUD
DataBase (MyDB) & Table (Kinds) From Inside SqlServer


Courses of Visual Basic online crud
Project Folder on Desktop With Database Inside

- What do we want to do ?!!!!

we want to update the database (MyDB), Table (Kinds) and Record (Kind) using DataGridView (DG1) From MS Visual Basic .NET 2005 on Windows XpSp2 Home Edition ........ ahahahaha, OK?!!

We will use NO buttons, we will only use DG1 Events :

  1. For Adding New Record : you type in the cell in the dg1 and hit Enter Key 
  2. For Editing : you Edit a cell that already have been saved and hit enter
  3. For Deleting : you Select a cell in the DG1 and you hit Delete key in the keyboard.

* Of course you can not add Empty Values because we have the record (Kind) is Primary Key (PK) .
* You can not Delete the (New DG1 Row) ...


All you have to do now is to open the Form1 Code and Clear all lines using (Ctrl+A) and (BackSpace) then Save Project (Ctrl+S) then Paste (Ctrl+V) these blocks of Codes :

Codes above works 100% just create the database and the table and the record successfully,


vb.net Datagridview source code download crud
vb.net Datagridview source code download
The example tested with vb2010

Function to Fill DataGridView with Table using DataTable

Imports System.Data.OleDb
Public Class FillDataGridView
  Public Shared Function FillDataGridFromTable(TableName As String) As DataTable
        Dim SqlStr As String = ("SELECT * FROM [" & TableName & "]")
    Dim MY_CONN_STR As String = MY_CONNECTIONSTRING_FROM_CONFIGFILES
        Using MyTable As DataTable = New DataTable
            Try
      Using Conn As New OleDbConnection With {.ConnectionString = MY_CONN_STR},
           CMD As New OleDbCommand(SqlStr, Conn)
                    Conn.Open()
                    Using MyReader As OleDbDataReader = CMD.ExecuteReader
                        MyTable.Load(MyReader)
                    End Using
                End Using
            Catch ex As OleDbException
                MsgBox(ex.Message)
            End Try
            Return MyTable
        End Using
    End Function
End Class
'Visual Basic .Net Form
Imports System.ComponentModel
Public Class Form1
'Visual Basic Controls on Form (Button, TextBox, DataGridView)
Private Sub ButtonFill_Click(sender As Object, e As EventArgs) Handles ButtonFill.Click
Dim ThisTable As String = TableTextBox.Text
            With DataGridView1
                .AllowUserToAddRows = False
                With .ColumnHeadersDefaultCellStyle
                    .Alignment = DataGridViewContentAlignment.MiddleCenter
                    .BackColor = Color.LightGray
                    .ForeColor = Color.DarkBlue
                End With
                .EnableHeadersVisualStyles = False
                .DataSource = DatabaseSettings.FillDataGridFromTable(ThisTable)
            End With
End Sub
End Class
      

Resize Image in DataGridView

Public Class Form1
  Private Sub DataGridView1_CellFormatting(sender As Object, _
      e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        If e.RowIndex = -1 Or e.ColumnIndex = -1 Then Exit Sub
        Dim Col_Name as String = ("ImageCol")
        Try
            If Not IsNothing(e.Value) Then
                If DataGridView1.Columns(e.ColumnIndex).ValueType = GetType(Byte()) Then
              Dim imageColumn = DirectCast(DataGridView1.Columns(Col_Name), DataGridViewImageColumn)
                    imageColumn.ImageLayout = DataGridViewImageCellLayout.Zoom
                End If
            End If
        Catch ex As Exception
            MsgBox("Error Display Image : " & ex.Message)
        End Try
    End Sub
End Class

 Here are some online Visual Basic lessons and courses:

Bottom Ad [Post Page]