[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

Working with Data Grids

I am having a little trouble working with Data grids.  I have the code to pull data from a DB using select statements and I put this data into a data table.

hen I set the DataGrid.datasource = to the data table.  It works fine.  My problem comes in when I try to edit the data.  I need help working with the data grid.  I want the user to double click the grid and have the row they double clicked appear in a new form for editing.  SO I take the value for each column in the ROW selected and place them in Text boxes/combo boxes and allow the user to edit.  Then when they save I want the updated data returned to the grid.  I have it all almost working now.  I know there is a better way but  Here is how I do it,

 When the user double clicks the grid I define a temp data table = DataGrid.datasource and set the current row of the temp table  = to DataGrid.currentRowIndex.  Then I pull the data from the Temp Table and populate the form.  When the data is saved I just update the temp table and set the DataGrid.datasource = to the temp table.  This is very sloppy I think.  But here is the real problem.  When the datagrid gets sorted by the user in some manor and I set the temp table = to the datagrid.datasource they are no longer sorted the same.  The temp table gets the original sort order and the currentRowIndex on the Grid no longer points to the correct row in my table.  I understand this problems comes from my poor method of handling the data grid and I would like a better solution and not a fix for my current method.  Thanks for any help.

  • 4
  • 3
1 Solution
ok your method looks ok except i would not use the temp table. so here is some pseudo code of how i would do it

'pull back from DB your data

'bind the table to the grid
YourDataGrid.Datasource = yourDataTable

'use binding manager base to preserve currentRowIndex when sorted
Dim bm As BindingManagerBase = YourDataGrid.BindingContext(YourDataGrid.DataSource, YourDataGrid.DataMember)

'when someone double clicks a row get the selected row and pass that to your editting form
Dim dr As DataRow
dr = CType(bm.current, DataRowView).Row

'this will pass the row object from your original datatable to the form
Dim YourEditForm As New YourEditForm(dr)

'make sure you add this to the new constructor in YourEditForm:
Public Sub New(dr as Datarow)
    localDataRow = dr
End Sub

'bind the datarow to your textboxes, and other editting controls, so when the values are changed in the text boxes the data table is updated automatically.
textbox1.DataBindings.Add("Text", dr.Table, "YourColumnName1")
textbox2.DataBindings.Add("Text", dr.Table, "YourColumnName2")

'save button on YourEditForm, this will save back to the database your changes, when you close the form the grid should reflect the changes

'as a side note you can also roll back changes not committed to the database by
'YourDataGrid will always be bound to YourDataTable so whatever changes occur in that will stay consistent
jtwestmoAuthor Commented:
A Couple of question for you

1.  I don't know much about the binding manager so the line
Dim bm As BindingManagerBase = YourDataGrid.BindingContext(YourDataGrid.DataSource, YourDataGrid.DataMember)
Am I just creating a binding object that links the grid datasource to ???.datamember?

2.  Question about scope.  Is the Dim bm As BindingManagerBase global to the entire form and is alive until the form goes away?   The same question about dr on the edit form.

Does dr have a link to the original grid becaseu we passed dr to the form byreference?

Your solution seems correct I just want to make sure I fully understand it.

the binding manager is an object that helps maintain the index when a datatable is bound to a datagrid. then you instantiate the bindingcontext for the datagrid you are pointing to what datasource and datamember the grid is bound to. this way the bindingmanager will be able to keep its index as different rows are selected.

as for the scope of the bindingmanager it depends on where throughout the form you need it. if you just need it in the double click even you can instantiate it just there it does not need to be global to the form. i usually keep everything as local as possble before increasing the scope.

the dr in the Edit Form should be global to the Edit form because it has to be passed by reference in the constructor. the dr is a pointer to a row in the datatable so changing its value will directly effect the datatable.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

jtwestmoAuthor Commented:
Just one more question though how do you go about adding a new data row.  Instead of editing exsiting data I want to add a new row.  DO you use bm.addnew  but then how to you specify for it to add the data row as the new odject?
no the bm is not used to add a new row. if you have allow edit enabled the datagrid will handle the addition of the a new row. the bm again will just point to that row the same way it does for existing rows. if you want to programmatically add a new row say using a button and then popping up a the EditForm you can do this:

Dim dr as DataRow

dr = YourDataTable.NewRow

'pass the new row to the Edit Form, and then bind the dr to the textboxes like i stated above
Dim YourEditForm as New YourEditForm(dr)

'when the ok or save button is clicked on the edit form you can then add the row to the datatable(you can also save to the database if you want at this point). but now the grid should have the new entry listed.
just so i don't confuse you, since dr is a reference to a row from YourDataTable, dr.Table is referencing YourDataTable
jtwestmoAuthor Commented:
You're exactly right, I don't know why I didn't see that.  That's so much for the help.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now