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
Solved

DataGridView: Updating DS after changes

Posted on 2006-11-01
7
506 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

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

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 …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 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