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
234 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: 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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…

724 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