Databinding & Datasets

Roey

Well-known member
Joined
Oct 10, 2002
Messages
238
Location
Canada
I have a Windows form which is populated by a SQL query via a Dataset. The dataset is used to populate a datagrid on the form, and lists the operations it takes to make a manufactured component, in a simplified manor. I want to be able to drag one of these operations, (a row on the datagrid) into a more detailed display of text boxes which can then be manipulated by the user.

So far I have worked out how to drag and drop the first field from the datagrid into one of the text boxes.

What I cant seem to work out is how to use this field to retrieve the appropriatte data from the dataset.

Thanks
 
Finding a specific row in a DataTable is, of course, a common application task. The Binding Context object doesnt directly support a Find method. Instead, you must use either a DataView object to find a row based on the current Sort key or use a DataTable object to find a row based on more complex criteria.

Using the DataViews Find method can be used only to find a row based on the row currently specified in the Sort property. If your datagrid is bound to a DataView, you can reference the object directly. If you bound the datagrid to a DataTable, you can use the DefaultView property to obtain a reference without creating a new object. The Find method returns the index of the row matching the specified criteria or -1 if no matching row is found. That index will correspond directly to the same rows index in the BindingContext object...so its simple to set the BindingContext.Postion property to that value and you are at that row. Textboxes etc. that are then bound to that datasource will display the data from the row found by the Find method.

Sample code:
Code:
Dim dv as System.Data.DataView = Me.dsMaster1.Categories.DefaultView replace with whatever your ds.db.DefaultView would be
Dim id As Integer
Dim idx As Integer

id = cint(textbox.text) via user input or from your dragged and dropped textbox, 

idx = dv.Find(id)

If idx = -1 Then 
MessageBox.Show("Category" + id.ToString + " not found", "Error")
Else
me.BindingContext(Me.dsMaster1, "Categories").Positon = idx
End If

If you need to find a row based on complex criteria, or on a single column other than the one on which the data is sorted, you must use the DataTables Select method.

Post me back here if you need an example, its easy to use, but postioning the CurrencyManager to the correct row requires several steps.

Jon
 
Thanks for your help Jon, I had a go at implementing the code however the code doesnt execute anything after the line :

idx = dv.Find(id)

The datagrid is bound to the dataset in the following manor:

dgrOperationList.DataSource = dsRoutingMaster.Tables("Routings").DefaultView


Thanks for your help
 
This block of code uses the DataTables Select method to find the specified name. Select returns an array of rows, the CType function converts the first row of the array to a RoutingsRow and sets its id to the PrimaryKey. It then finds the PrimaryKey in the DataView and postions the BindingContext.Postion.

Code:
Dim dv as System.Data.DataView = dsRoutingMaster.Tables("Routings").DefaultView
Dim dt As System.Data.DataTable = dsRoutingMaster.Tables("Routings")
Dim dr() As System.Data.DataRow
Dim name1 As String
Dim id As Integer
Dim idx As Integer

name1 = textbox.txt via user input or from your dragged and dropped textbox, 

dr = dt.Select("[b]Column_name_you_are_searching_on[/b] = " & name1 & "" substitute in the ColumnName 

id = CType(dr(0), dsRoutingMaster.RoutingsRow).[b]CategoryID[/b] Here, youll need to substitute the PrimaryKey column

idx = dv.Find(id)

If idx = -1 Then 
MessageBox.Show("Name" + id.ToString + " not found", "Error")
Else
me.BindingContext(dsRoutingMaster, "Routings").Positon = idx
End If

Walk through it with your intellisense on and give it a whrill.


Jon
 
Thanks again jfakler. I got it to work using the following code:

_________________________________________________
Dim dv As System.Data.DataView = dsRoutingMaster.Tables("Routings").DefaultView
Dim id As Integer
Dim idx As Integer

txtOperationNo.Text = e.Data.GetData(DataFormats.Text).ToString which text box receives the data
id = CInt(txtOperationNo.Text) via user input or from your dragged and dropped textbox,
dv.Sort = "OperationNo"
idx = dv.Find(id)

If idx = -1 Then
MessageBox.Show("Category" + id.ToString + " not found", "Error")
Exit Sub
Else
Me.BindingContext(Me.dsRoutingMaster.Tables("Routings")).Position = idx
End If

txtOperationDescription.DataBindings.Add("Text", dsRoutingMaster.Tables("Routings"), "OperationDescription")
cboCostCentre.DataBindings.Add("Text", dsRoutingMaster.Tables("Routings"), "CCDescription")
cboWorkCentre.DataBindings.Add("Text", dsRoutingMaster.Tables("Routings"), "WCDescription")
cboMachine.DataBindings.Add("Text", dsRoutingMaster.Tables("Routings"), "MCDescription")
txtSetUpTime.DataBindings.Add("Text", dsRoutingMaster.Tables("Routings"), "SetUpTime")
txtEstimatedTime.DataBindings.Add("Text", dsRoutingMaster.Tables("Routings"), "EstimatedTime")
txtStandardTime.DataBindings.Add("Text", dsRoutingMaster.Tables("Routings"), "StandardTime")
txtComments.DataBindings.Add("Text", dsRoutingMaster.Tables("Routings"), "Comments")
____________________________________________

The line that seemed to get it working was the dv.sort, ant comments or suggestions you have about this code would be greatly appreciatted.

Dave
 
Using the DataViews Find method can be used only to find a row based on the row currently specified in the Sort property. Thus the necessity for the sort. Looks good.

Jon
 
Back
Top