Solved

how can I Add new Rows to Datagridview dynamically and these new rows should be updated in database table automatically just like in datagrid in VB6

Posted on 2008-10-29
6
228 Views
Last Modified: 2012-05-05
I am new to VB 2005. I have created a new form in VB.NET 2005 and placed  datagridview on the form.
its property AllowUserToAddRows is set to true and placed a button on form. It shows data on the grid properly and allows to add rows but these rows are not reflected in database table.  how can I Add new Rows to Datagridview dynamically and these new rows should be Added in database table automatically just like in datagrid in VB6. I also want whenever any cell is updated its values should be updated in database table. How can we make some columns editable and others are not.


Imports System.Data.SqlClient

Public Class Form1

    Private LawCon As SqlConnection

    Private LawDa As SqlDataAdapter

    Private LawDs As DataSet

    Private Lawcmd As SqlCommand
 
 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        LawCon = New SqlConnection("server=MITTAL;uid=sa;pwd=;database=mys")

        LawDa = New SqlDataAdapter("select * from Law1", LawCon)

        LawDs = New DataSet()

        LawDa.Fill(LawDs, "Laws")       

        DataGridView1.DataSource = LawDs.Tables("Laws")

        DataGridView1.Refresh()
 
 

    End Sub

Open in new window

0
Comment
Question by:mittalmunish80
  • 3
6 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 22839046
You need to setup a DataTableAdapter to achieve this functionality. You would configure this adapter by providing the appropriate SELECT, UPDATE, INSERT commands along with their parameters. Most of the times, you just need to provide the select command and the adapter automatically generates the other commands for you if the table has a primary key. You also need a dataset to hold the values in memory and you may be using it already with your datagridview. When properly configured, calling the DataTableAdapter.Update(DataSet) function saves all the changes (inserts, deletes, modifications) to the database.

Hope it helps
Code Cruiser
0
 

Author Comment

by:mittalmunish80
ID: 22839232
Can you tell me through sample code or by updating my code
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 22839323
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 23050405
Is there a way of changing the zone? This is a VB.NET question and VB.NET is among the selected zones but i was awarded points in the Languages zone!!!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

758 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

19 Experts available now in Live!

Get 1:1 Help Now