WPF Datagrid?
-
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
-
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
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
-
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
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??
-
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??
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
-
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
-
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??
-
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??
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
-
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
-
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??
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.
-
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.
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