Solved

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

Posted on 2014-03-10
3
13,815 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

910 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

24 Experts available now in Live!

Get 1:1 Help Now