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
229 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

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
This video teaches viewers about errors in exception handling.

864 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

23 Experts available now in Live!

Get 1:1 Help Now