Solved

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

Posted on 2014-03-10
3
14,975 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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