Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. WPF Datagrid?

WPF Datagrid?

Scheduled Pinned Locked Moved WPF
csharpcssasp-netdatabasewpf
10 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    S Rajput
    wrote on last edited by
    #1

    Hello, What i am doing is, i am creating a Datatable based on values that user fills up in the form. Further i am assigning that datatable to the WPF datagrid to display the Grid. dgMatrix.ItemsSource = GetDataSource.DefaultView Now, as the user edits the grid and presses the SAVE button under the grid, I would like to save all the rows of the Grid into a new table in the database. Is there a way to loop through each row of the Grid and then inserting each record into the database?? In ASP.net i was using the following function: 'For Each row As GridViewRow In dgMatrix.Rows ' ' Dim checkbox As CheckBox = CType(row.FindControl("cbRows"), CheckBox) ' If checkbox.Checked Then ' Try ' Dim txtFromPorts As TextBox = CType(row.FindControl("txtPortsFrom"), TextBox) ' Dim txtToPorts As TextBox = CType(row.FindControl("txtPortsTo"), TextBox) ' Dim txtFType As TextBox = CType(row.FindControl("txtFareType"), TextBox) ' Dim txtFClass As TextBox = CType(row.FindControl("txtFareClass"), TextBox) ' Dim txtLowGAmt As TextBox = CType(row.FindControl("txtGrossAmtLow"), TextBox) ' Dim txtLowNAmt As TextBox = CType(row.FindControl("txtNettAmtLow"), TextBox) ' Dim txtHighGAmt As TextBox = CType(row.FindControl("txtGrossAmtHigh"), TextBox) ' Dim txtHighNAmt As TextBox = CType(row.FindControl("txtNettAmtHigh"), TextBox) ' Dim txtPeakGAmt As TextBox = CType(row.FindControl("txtGrossAmtPeak"), TextBox) ' Dim txtPeakNAmt As TextBox = CType(row.FindControl("txtNettAmtPeak"), TextBox) ' ' Dim dr As DataRow = dtNew.NewRow ' strFrom = txtFromPorts.Text ' strTo = txtToPorts.Text ' strFareType = txtFType.Text ' If txtFClass.Text.Trim <> "" Then ' strFareClass = txtFClass.Text ' End If ' If txtLowGAmt.Text <> "" Then ' strGrossLow = txtLowGAmt.Text ' End If ' If txtLowNAmt.Text <> "" Then ' strNettLow = txtLowNAmt.Text ' End If ' If txtHighGAmt.Text <> "" Then ' strGrossHigh = txtHighGAmt.Text ' End If

    C 1 Reply Last reply
    0
    • S S Rajput

      Hello, What i am doing is, i am creating a Datatable based on values that user fills up in the form. Further i am assigning that datatable to the WPF datagrid to display the Grid. dgMatrix.ItemsSource = GetDataSource.DefaultView Now, as the user edits the grid and presses the SAVE button under the grid, I would like to save all the rows of the Grid into a new table in the database. Is there a way to loop through each row of the Grid and then inserting each record into the database?? In ASP.net i was using the following function: 'For Each row As GridViewRow In dgMatrix.Rows ' ' Dim checkbox As CheckBox = CType(row.FindControl("cbRows"), CheckBox) ' If checkbox.Checked Then ' Try ' Dim txtFromPorts As TextBox = CType(row.FindControl("txtPortsFrom"), TextBox) ' Dim txtToPorts As TextBox = CType(row.FindControl("txtPortsTo"), TextBox) ' Dim txtFType As TextBox = CType(row.FindControl("txtFareType"), TextBox) ' Dim txtFClass As TextBox = CType(row.FindControl("txtFareClass"), TextBox) ' Dim txtLowGAmt As TextBox = CType(row.FindControl("txtGrossAmtLow"), TextBox) ' Dim txtLowNAmt As TextBox = CType(row.FindControl("txtNettAmtLow"), TextBox) ' Dim txtHighGAmt As TextBox = CType(row.FindControl("txtGrossAmtHigh"), TextBox) ' Dim txtHighNAmt As TextBox = CType(row.FindControl("txtNettAmtHigh"), TextBox) ' Dim txtPeakGAmt As TextBox = CType(row.FindControl("txtGrossAmtPeak"), TextBox) ' Dim txtPeakNAmt As TextBox = CType(row.FindControl("txtNettAmtPeak"), TextBox) ' ' Dim dr As DataRow = dtNew.NewRow ' strFrom = txtFromPorts.Text ' strTo = txtToPorts.Text ' strFareType = txtFType.Text ' If txtFClass.Text.Trim <> "" Then ' strFareClass = txtFClass.Text ' End If ' If txtLowGAmt.Text <> "" Then ' strGrossLow = txtLowGAmt.Text ' End If ' If txtLowNAmt.Text <> "" Then ' strNettLow = txtLowNAmt.Text ' End If ' If txtHighGAmt.Text <> "" Then ' strGrossHigh = txtHighGAmt.Text ' End If

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      I believe the idea is that updates to the grid automatically update the data source, and so you should iterate over that.

      Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

      S 1 Reply Last reply
      0
      • C Christian Graus

        I believe the idea is that updates to the grid automatically update the data source, and so you should iterate over that.

        Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

        S Offline
        S Offline
        S Rajput
        wrote on last edited by
        #3

        Thanks for reply Christian.This is what i have done, dvMatrix = GetDataSource.DefaultView dgMatrix.ItemsSource = dvMatrix Datasource1 = GetDataSource() So, I am assigning itemsource using GetDataSource method which returns me a datatable. I will store it in a public datatable variable(DataSource1). So inside the btnSave_click event, Dim dt As DataTable Dim dr1 As DataRow dt = Datasource1 'i am using the datatable- DataSource1 Dim jk = dt.Rows.Count For Each dr1 In dt.Rows strFrom = dr1("PortsFrom") strTo = dr1("PortsTo") .... next Problem is, i am still getting original grid content not the modified content. Is there something i need to do inside dgMatrix_RowEditEnding function to update the datasource??

        C 1 Reply Last reply
        0
        • S S Rajput

          Thanks for reply Christian.This is what i have done, dvMatrix = GetDataSource.DefaultView dgMatrix.ItemsSource = dvMatrix Datasource1 = GetDataSource() So, I am assigning itemsource using GetDataSource method which returns me a datatable. I will store it in a public datatable variable(DataSource1). So inside the btnSave_click event, Dim dt As DataTable Dim dr1 As DataRow dt = Datasource1 'i am using the datatable- DataSource1 Dim jk = dt.Rows.Count For Each dr1 In dt.Rows strFrom = dr1("PortsFrom") strTo = dr1("PortsTo") .... next Problem is, i am still getting original grid content not the modified content. Is there something i need to do inside dgMatrix_RowEditEnding function to update the datasource??

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Your datasource needs to be an observablecollection for this to work. Perhaps it's not.

          Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

          S 1 Reply Last reply
          0
          • C Christian Graus

            Your datasource needs to be an observablecollection for this to work. Perhaps it's not.

            Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

            S Offline
            S Offline
            S Rajput
            wrote on last edited by
            #5

            Excellent,with observablecollection its working. Thanks a ton for guiding me!!! :-D

            S 1 Reply Last reply
            0
            • S S Rajput

              Excellent,with observablecollection its working. Thanks a ton for guiding me!!! :-D

              S Offline
              S Offline
              S Rajput
              wrote on last edited by
              #6

              Just because i thought it will help me in future. If we fetch data from some table in database, into a observable collection. And then display that data in the grid. When the user edits the grid, changes are reflected in observable collection, is there a quick way to commit those changes in the database as well??

              C P 2 Replies Last reply
              0
              • S S Rajput

                Just because i thought it will help me in future. If we fetch data from some table in database, into a observable collection. And then display that data in the grid. When the user edits the grid, changes are reflected in observable collection, is there a quick way to commit those changes in the database as well??

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                I don't really know, to be honest. I've not done any database work with WPF yet, that is nothing to a SQL database, all to my own file formats. I would imagine you'd want to make an observablecollection of a class type, and write code in there to update the DB if data is changed, or new data added, but that could be old school thought, there may be something better built in.

                Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

                S 1 Reply Last reply
                0
                • C Christian Graus

                  I don't really know, to be honest. I've not done any database work with WPF yet, that is nothing to a SQL database, all to my own file formats. I would imagine you'd want to make an observablecollection of a class type, and write code in there to update the DB if data is changed, or new data added, but that could be old school thought, there may be something better built in.

                  Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

                  S Offline
                  S Offline
                  S Rajput
                  wrote on last edited by
                  #8

                  I have the same feeling,there must be a better way of doing it. Thanks for your help though!

                  1 Reply Last reply
                  0
                  • S S Rajput

                    Just because i thought it will help me in future. If we fetch data from some table in database, into a observable collection. And then display that data in the grid. When the user edits the grid, changes are reflected in observable collection, is there a quick way to commit those changes in the database as well??

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    Any time you add an item into an observable collection, or remove an item, the collection raises a CollectionChanged event. You can use this to identify records that have been added or deleted. If you are updating a record though, the collection won't help you because it doesn't monitor changes to items in the collection; it only monitors changes to the collection itself. What you need to do, in this case, is have your model expose INotifyPropertyChanged, and then (whenever a property is changed), raise a change notification on the item itself. This[^] article gives a bit more detail about notifications.

                    "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                    As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                    My blog | My articles | MoXAML PowerToys | Onyx

                    C 1 Reply Last reply
                    0
                    • P Pete OHanlon

                      Any time you add an item into an observable collection, or remove an item, the collection raises a CollectionChanged event. You can use this to identify records that have been added or deleted. If you are updating a record though, the collection won't help you because it doesn't monitor changes to items in the collection; it only monitors changes to the collection itself. What you need to do, in this case, is have your model expose INotifyPropertyChanged, and then (whenever a property is changed), raise a change notification on the item itself. This[^] article gives a bit more detail about notifications.

                      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                      My blog | My articles | MoXAML PowerToys | Onyx

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #10

                      See - I knew there was a smarter answer....

                      Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups