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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calculate number of nights between two dates 5 86
ASP.NET (VB) return a record 2 51
Link failure 16 60
VB.net Excel VSTO Add-in version compatability 2 12
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

685 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