Solved

DataGridView: Updating DS after changes

Posted on 2006-11-01
7
497 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

19 Experts available now in Live!

Get 1:1 Help Now