VB .NET CRUD
Create, Read, Update, Delete
DATAGRIDVIEW with SQL Server
Note
If you don't know how to work SQL Server With VB .NET : You need to see this then thisLogic 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
- Create a new Visual Basic project (Windows Application) name it as (MyDg1) and save it to your hard drive.
- Place a DataGridView Control on the Form1.
- Create SQL Server Database file, name it (MyDB)
- Create Table, name it (Kinds) with just one record, name it (Kinds)
- Rename the DataGridView Control's name From (DataGridView1) to (DG1)
- Save/Build the project .
- Now you have: A project such as this one in the 3 photos (from inside VB .NET and from Outside)
vb.net DataGridView Project From Inside VB.Net 2005 |
DataBase (MyDB) & Table (Kinds) From Inside SqlServer |
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?!!
- For Adding New Record : you type in the cell in the dg1 and hit Enter Key
- For Editing : you Edit a cell that already have been saved and hit enter
- 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 :
vb.net Datagridview source code download |
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: