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
233 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
[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
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

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