Visual Basic Online Courses DataGridView Add Edit Delete

VB .NET DataGridView Add Edit Delete

Manipulate DataGridView from Visual Basic 2010 with SQL Server

DataGridView Add, Edit, Delete From Database
Visual Basic Online Course - DataGridview SQL Server Database
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
DataBase (MyDB) & Table (Kinds) From Inside SqlServer

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

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

we want to update the database (MyDB) Table (Kinds) Record (Kind) using DataGridView (DG1) From MS Visual Basic .Net 2005 on WinXpSp2 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
vb.net Datagridview source code download
The example tested with vb2010


Download Full Example : MediaFire.com Link


Function to Fill DataGridView with Table using DataTable
'Visual Basi .Net Class
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
view raw Datagridview.vb hosted with ❤ by GitHub

Resize Image in DataGridView
'Visual Basic Form
'Form1 (DataGridView1)
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 :

Popular posts from this blog

VB .NET DropBox Api Source Code Example

VB .NET Google Drive Api Source Code Example

VB.NET Access 2007 Hierarchical TreeView

VB.NET How to properly close a windows application Form

DAO in VB .NET MS ACCESS Database

Solution - There is already an open DataReader associated with this Command which must be closed first.