1 Replies - 516 Views - Last Post: 10 October 2017 - 01:59 PM Rate Topic: -----

#1 nighttrain   User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 485
  • Joined: 22-September 10

Update Dataset binded to datagridview

Posted 10 October 2017 - 01:41 PM

In my datagridview which is bound to dataset users can make some changes. Not all fields from dataset are possible to be changed by users.

Everything is my DAL class

This is what i use to bind datagridview:

at the top of dal class:

Public ReadOnly GetArtikelDataSet As New DataSet

Bind grid to this:
Public Function GetDataSet() As DataSet Implements IDbManipulation.GetDataSet
       Using con As New SqlConnection(_strcon)
             Using cmd As New SqlCommand("SELECT Art.Id, Art.Nummer, Art.Serie, Art.EANBarcode, Art.Preis, Art.[User], Art.Vater, Art.Name, Art.Amazon, Kat.Name As Kategorie, Subkat.Name As Subkategorie, ISNULL(Subsubkat.Name,'') As SubSubkategorie, Art.FK_Geschaft_ID, Art.CreateDate FROM T_Article As Art " &
                                                    "INNER JOIN T_Kategorie As Kat ON Art.FK_Kategorie_ID=Kat.Id " &
                                                    "INNER JOIN T_Subkategorie As Subkat ON Art.FK_SubKategorie_ID=Subkat.Id " &
                                                    "LEFT OUTER JOIN T_SubSubKategorie As Subsubkat ON Art.FK_SubSubKategorie_ID=Subsubkat.Id ORDER BY Art.Vater ASC",
                                                    con) ' order by
                Using getProjectsDataAdapter = New SqlDataAdapter(cmd)
                   getProjectsDataAdapter.Fill(GetArtikelDataSet, eArticle.Main.ToString)
                End Using
           End Using
      End Using

Return GetArtikelDataSet
End Function

This is how i check whether any change to dasaource:

Public Function CheckChangesDataSet() As Boolean
      Dim result = False
         If GetArtikelDataSet.HasChanges Then
                result = True
         End If
    Return result
End Function

If above method gives me true then i do this:

Public Sub MakeChangesDataSet() Implements IDbManipulation.MakeChangesDataSet
            If GetArtikelDataSet.Tables(0).Rows.Count > 1 Then
            End If
            Using myConnection = New SqlConnection(_strcon)

                Using cmd As New SqlCommand("SELECT Id, Serie, EANBarcode, Preis, Name, Amazon, FK_Geschaft_ID FROM T_Article ORDER BY ID ASC",
                    'Create a data adapter in the method and throw it away afterwards
                    Using getProjectsDataAdapter = New SqlDataAdapter(cmd)
                        Dim cmdbuilder As New SqlCommandBuilder(getProjectsDataAdapter)
                        getProjectsDataAdapter.Update(GetArtikelDataSet, eArticle.Main.ToString)
                    End Using
                End Using
            End Using
End Sub

All questions related:

1. Is it correct as i did that in MakeChangesDataSet() having just fields which could be changed by users in grid or do i have to type exactly the same query as was in GetDataSet?
2. Should both methods: GetArtikelDataSet and MakeChangesDataSet share same SqlDataAdapter? As you see i use diffrent for one method and another. Shouldn't adapter be defined and shared on class level so both methods access the same?
3. Not sure but read somwhere that Updatecommand has to be associated with sqldataadapter but have no idea what does it mean.
4. I heard that even if i do Update on data adapter it could choose either insert/update/delete - in my case grid could be just modified (no possibility to insert rows by user or delete) i would like (just in case to restrict sqladapter to make insert/delete) how to do that?

Mainly asking all of this because sometimes seems that not every time data is updated.

This post has been edited by nighttrain: 10 October 2017 - 01:56 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Update Dataset binded to datagridview

#2 modi123_1   User is online

  • Suitor #2
  • member icon

Reputation: 14158
  • View blog
  • Posts: 56,748
  • Joined: 12-June 08

Re: Update Dataset binded to datagridview

Posted 10 October 2017 - 01:59 PM

Why not just get the changed rows and use an UPDATE statement?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1