Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

What's 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

  • *Experts*
Posted

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 it's own sorting) and unfiltered.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

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

 

Me.BindingContext(dsCountry.Tables("Country")).AddNew()

  • *Experts*
Posted

I would say "yes" it's Ok, but it's hard to say without trying it out. I've 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 don't know what the AddNew is doing it's 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 aren't set that would allow it to be included in the filter.

 

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

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

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.

  • *Experts*
Posted

In the "New" button, add a row to the DataTable (as it sounds like you did), but don't change any control's 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:

'maybe Count - 1...
BindingContext(dsCountry.Tables("Country").DefaultView).Position = dsCountry.Tables("Country").DefaultView.Count;

 

If you don't reposition the BindingContext, you'll 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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

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 I'm being a bit dim with this !!!!

  • *Experts*
Posted

If you're using Bound Controls, you don't 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 you're allowing the user to press a "New Row" button, you're letting them add multiple rows and change multipe rows and then saving ALL the changes at once.

 

First, are you using Bound controls? Second, what's 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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...