defaultview and binding

Roey

Well-known member
Joined
Oct 10, 2002
Messages
238
Location
Canada
Whats the difference between binding a dataset using:

cboCountryCode.DataSource = dsCountry.Tables("Country")
cboCountryCode.DisplayMember = "CountryCode"
txtDescription.DataBindings.Add("Text", dsCountry.Tables("Country"), "Description")


and using the defaultview option

cboCountryCode.DataSource = dsCountry.Tables("Country").DefaultView
cboCountryCode.DisplayMember = "CountryCode"
txtDescription.DataBindings.Add("Text", dsCountry.Tables("Country").DefaultView, "Description")

Thnaks in advance
 
Thnaks? Sounds like a yummy snack :)

The DefaultView is a built-in DataView. That means you can bind to a DataView and later add a RowFilter or a Sort without rebinding. Binding to a DataTable means always showing all records, unsorted (unless you sort in the DB or your control allows its own sorting) and unfiltered.

-Nerseus
 
Can I still add a new record in the same way if binding my data to the defaultview

Me.BindingContext(dsCountry.Tables("Country")).AddNew()
 
I would say "yes" its Ok, but its hard to say without trying it out. Ive never tried to do AddNew on the BindingContext object, I always go through the table (ds.Tables[...].Rows.Add()). I know the BindingContext array (or indexer) sees the table differently than the DefaultView of the table and will give you two different references, but since I dont know what the AddNew is doing its hard to guess what will happen :)

If the AddNew is adding a new, empty row to your DataTable, then I would think it would be fine. Depending on your RowFilter, the new row might not show up if the initial values arent set that would allow it to be included in the filter.

If you dont plan on filtering or sorting, I would bind directly to the DataTable. The DefaultView has a tiny bit of overhead since its essentially an array into the real DataTable to allow filtering and sorting.

-Nerseus
 
I did try using the rows add initially in my code. I would cleari the combobox and the text box on the form by setting their text values to "" upon the click event of a New button, and the adding the rows to the datatable as follows using the click event of a Save button:

NewRow.Item("CountryCode") = cboCountryCode.Text
NewRow.Item("Description") = txtDescription.Text
dsCountry.Tables("Country").Rows.Add(NewRow)


However the textdescription would add against the first record of the datatable, and there would be no record of the newly input Country code.
 
In the "New" button, add a row to the DataTable (as it sounds like you did), but dont change any controls properties. Instead, set the position property of the binding context to the new row, the bound controls will clear automatically (or will show any default values in your DataSet).

To set the position, use:
Code:
maybe Count - 1...
BindingContext(dsCountry.Tables("Country").DefaultView).Position = dsCountry.Tables("Country").DefaultView.Count;

If you dont reposition the BindingContext, youll always be pointing to the first row in the DataTable. If you have next/prev buttons, you can have them increase and decrease the Position property.

-Nerseus
 
Thanks for all your help Nerseus, if I declare the NewRow under the code for the new button click as follows:

Private Sub cmdNew_Click

Dim NewRow As DataRow = dsCountry.Tables("Country").NewRow

BindingContext(dsCountry.Tables("Country").DefaultView).Position = dsCountry.Tables("Country").DefaultView.Count


How would I reference it in the command Save button as NewRow has not been declared there.

Private Sub cmdSave_Click

NewRow.Item("CountryCode") = cboCountryCode.Text NewRow.Item("Description") = txtDescription.Text dsCountry.Tables("Country").Rows.Add(NewRow)


Sorry if Im being a bit dim with this !!!!
 
If youre using Bound Controls, you dont care about the controls in the Save button. As changes are made to the controls the new values are automatically saved in the DataSet. You can get the current row through the BindingContext if you need it (instead of using the NewRow variable). Normally if youre allowing the user to press a "New Row" button, youre letting them add multiple rows and change multipe rows and then saving ALL the changes at once.

First, are you using Bound controls? Second, whats the scope of a transaction? Meaning, is a transaction just ONE insert/update/delete, or is it multiple records? Can it be multiple records from multiple tables, or just from one table?

-Nerseus
 
Yes, they are bound controls, one combobox and one textbox. I am trying to create a single transaction, one record being updated, added or deleted. I was under the impression that I had to call the dataadapter.update function to update the database.
 
Back
Top