How to Add,Delete and update from datagridview

Hi All,

I am new in VB.Net and in DataGridView. Below is my code:

Option Explicit On

Imports System.Data

Imports System.Drawing

Imports System.Data.SqlClient

Public Class frmDrinkMenu

Private Sub frmDrinkMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Call startup()

Me.MenuDataGridView.DataSource = Me.MenuBindingSource

showdata("select * from drinkmenu ")

With MenuDataGridView

.EditMode = DataGridViewEditMode.EditOnEnter

.Name = "MenuDataGridView"

.Location = New Point(8, 8)

.Size = New Size(500, 300)

.AutoSizeRowsMode = _

DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders

.ColumnHeadersBorderStyle = _

DataGridViewHeaderBorderStyle.Raised

.CellBorderStyle = _

DataGridViewCellBorderStyle.Single

.GridColor = SystemColors.ActiveBorder

.RowHeadersVisible = True

.ScrollBars = ScrollBars.Both

.ColumnHeadersDefaultCellStyle.Alignment= DataGridViewContentAlignment.MiddleCenter

.Columns(0).DataPropertyName = "drink_code"

.Columns(0).Name = "drink_cd"

.Columns(0).HeaderText = "Drink Code"

.Columns(1).DataPropertyName = "drink_desc"

.Columns(1).Name = "drink_name"

.Columns(1).HeaderText = "Description"

.Columns(2).DataPropertyName = "print_name"

.Columns(2).Name = "printname"

.Columns(2).HeaderText = "Receipt Name"

.Columns(3).DataPropertyName = "price"

.Columns(3).Name = "price"

.Columns(3).HeaderText = "Price"

.SelectionMode = _

DataGridViewSelectionMode.FullRowSelect

.MultiSelect = False

.BackgroundColor = Color.Honeydew

.Dock = DockStyle.Fill

End With

End Sub

Private Sub showdata(ByVal selectcommand As String)

Try

Dim dataAdapter As New SqlDataAdapter(selectcommand, SQLConn)

Dim commandbuilder As New SqlCommandBuilder(dataAdapter)

Dim Menutable As New DataTable()

Menutable.Locale = System.Globalization.CultureInfo.InvariantCulture

dataAdapter.Fill(Menutable)

MenuBindingSource.DataSource = Menutable

Catch ex As SqlException

MessageBox.Show(Err.Description)

End Try

End Sub

OK. the problem is how i want to save,update to database and Delete records from database by using bindingNavigator. I have check many code sample but I can't really understand the code and modify to to fit for my code. Any body can give me some sample code which is can use for my current code.

One of the problem also is I don't knowhow t get the value for the cell or rows. Maybe is the declaration there still not really understand it.

I hope can get some guide here coz I am urgently need to finishmy program.

Thanks for advance.



Answer this question

How to Add,Delete and update from datagridview

  • How to Add,Delete and update from datagridview