Solved

DataGridView: Updating DS after changes

Posted on 2006-11-01
7
507 Views
Last Modified: 2008-02-26
Hello. I'm working with Datagridview, loading and binding to a dataset created on the fly.

        Public Function CropMasterSelect(ByVal WhereClause As String) As DataSet
            With cmd
                .Connection = con
                .CommandText = "admCropMasterSEL"
                .Parameters.Clear()
                .Parameters.AddWithValue("@WhereClause", WhereClause)
                .CommandType = CommandType.StoredProcedure
            End With
            da.SelectCommand = cmd
            ds.Tables.Clear()
            da.Fill(ds)
            Return ds

        End Function

        Me.DataGridView1.DataSource = db.CropMasterSelect(CreateWhereClause).Tables(0)
        Me.DataGridView1.Columns(0).Visible = False

My question is, after changes have been made in DataGridView by the user, how do I update those records back to the database?  Can I somehow use the dataset that was created on the fly?  Any code examples would be helpful.

Regards,
ADWooley
0
Comment
Question by:adwooley2
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Accepted Solution

by:
ericwong27 earned 300 total points
ID: 17848278

       ' Retrieve source data
        Dim lDataTable As DataTable = CropMasterSelect(WhereClause).Table(0)
        lDataTable.AcceptChanges()

        ' Assume data has bee change
        lDataTable.Rows(0)(1) = "xxx"

        ' Update the changes
        For Each lRows As DataRow In lDataTable.GetChanges.Rows
            ' Write a code to update the changed
        Next
0
 
LVL 5

Expert Comment

by:DEEPESH
ID: 17848763
Its all about AcCeptChanges, this command will save bach changes to Database.
http://msdn2.microsoft.com/en-gb/library/system.data.dataset.acceptchanges.aspx
0
 
LVL 10

Expert Comment

by:ericwong27
ID: 17848856
Hi, DEEPESH

AcceptChanges just commit the change on dataset but not in database. To update the data on database need to use DataAdapter.Update(ds) but since store procedure is used is not applicable here.



0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:DEEPESH
ID: 17849087
Hmmm
Sorry EricWong27 I forget to mention the Update command, but in this kind of scenario I think we have to make an seprate  update query and update database
0
 

Author Comment

by:adwooley2
ID: 17854627
Thanks for your comments.
Eric, I can't use DataAdapter.Update(ds) when using a stored procedure? I am forced to iterate through the datarows and update with changes one by one?
0
 
LVL 10

Expert Comment

by:ericwong27
ID: 17856158
Yes ... DataAdapter.Update(ds) has some limitation, you should not use store procedure, make a query from more then 1 table and etc
0
 

Author Comment

by:adwooley2
ID: 17856166
Eric,
Thanks.  I did solve my problem.  However, I'm having another related problem.  
I posted a new question:  Datagridview - update problems

If you can help me, I would be appreciative.
Regards,
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

713 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