Solved

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

Posted on 2014-03-10
3
13,445 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
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
Comment Utility
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
Comment Utility
Wow that works great.
0
 

Expert Comment

by:Nash Bawz
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now