Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 16427
  • Last Modified:

VB.Net - Update, Delete, and Insert from datagrid

I created a windows form using Visual Studio 2005 with a datagridview.  I want my user to be able to delete the row, make an update, and create new records within the datagridview.

I googled and came across an example, but not too familiar with C programming.  Can someone provide an example using VB?

http://www.codeproject.com/Articles/20456/Updatable-and-reusable-DataGridView-for-Oracle-or

My datagrid display just the ID and Description.  I want to be able to delete the rows, insert new rows, but can only update the Description column.
0
holemania
Asked:
holemania
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
Well, for a very simple editable grid, you'd be looking at something like the following (assuming a DataGridView called dataGridView1 and two buttons named SaveButton and DeleteButton):
Imports System.Data.SqlClient

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        DataGridView1.AllowUserToAddRows = True
        DataGridView1.AllowUserToDeleteRows = True

        Dim cn As SqlConnection = New SqlConnection("Server=(local);Initial Catalog=CarlDemo;Integrated Security=SSPI;")
        adap = New SqlDataAdapter("SELECT ItemID, Description FROM TestTable", cn)

        Dim builder As New SqlCommandBuilder(adap)
        adap.InsertCommand = builder.GetInsertCommand()
        adap.UpdateCommand = builder.GetUpdateCommand()
        adap.DeleteCommand = builder.GetDeleteCommand()

        dt = New DataTable()
        adap.Fill(dt)

        DataGridView1.DataSource = dt

    End Sub

    Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click

        Try
            adap.Update(dt)
            MessageBox.Show("Saved successfully")
        Catch ex As Exception
            MessageBox.Show("Error updating database")
        End Try

    End Sub

    Private Sub DeleteButton_Click(sender As Object, e As EventArgs) Handles DeleteButton.Click

        Try
            DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(0).Index)
        Catch ex As Exception
            MessageBox.Show("Error removing row")
        End Try

    End Sub

    Private adap As SqlDataAdapter
    Private dt As DataTable

End Class

Open in new window

1
 
holemaniaAuthor Commented:
Wow that works great.
0
 
Nash BawzCommented:
It works but does not update MySql database ( from where I am retrieving my table for data grid view). Could u plsc tell me the procedure too date MySql database with the above mentioned save  button.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now