Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-03-10
3
Medium Priority
?
16,061 Views
Last Modified: 2016-08-14
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
Comment
Question by:holemania
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 39918016
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
 

Author Comment

by:holemania
ID: 39920710
Wow that works great.
0
 

Expert Comment

by:Nash Bawz
ID: 41755446
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question