Adding a Data Row

R

Roshawn

Hi,

I have a form with textboxes that are bound to an untyped dataset (via the
BindingContext object). I also use the form's CurrencyManager and have
navigation buttons on the form. When the form is loaded, all data is
retrieved from the db and is displayed correctly in the textboxes. I'm also
able to navigate efficiently.

I'm having trouble adding a new row to my data table. Here's my code:

Private Sub AddData()
Dim dr As DataRow = ds.Tables("Members").NewRow()
With dr
.Item("FName") = txtFName.Text
.Item("LName") = txtLName.Text
.Item("Address") = txtAddress.Text
.Item("City") = txtCity.Text
.Item("State") = txtState.Text
.Item("Zip") = txtZip.Text
End With
ds.Tables("Members").Rows.Add(dr)
End Sub

The problems are:

1) the position of the CurrencyManager does not initally move to the new
row; instead it remains on the current row

2) the datarow is added to the table without permitting me to specify
desired data in the textboxes (I was under the impression that I could
modify data in the textboxes before the row was added to the table); instead
it just copies the data of the current row, thus creating duplicate data

What am I doing wrong? Is this the correct way to add a new row to a table
in a dataset or is there another way to do this? Any help will be
appreciated.

Thanks,
Roshawn
 
W

William Ryan eMVP

Roshawn:

You can accomplish the same thing by calling the .AddNew Method of the
CurrencyManager/BindingManagerBase . If nothing else, it reduces the amount
of code and will effectively 'clear' out the existing data so you can enter
the new stuff.

If you call AddNew, then you can edit the data however you please,
programmatically of via the GUI, and the position will move accordingly and
the rowstate will be marked correctly (although this last one should be the
case as things stand now.)

HTH,

Bill
 
R

Roshawn

Thanks a lot, Bill! Your solution was just what I needed. But out of
curiosity, are there any performance penalties associated with using this
method, or is it the same as using the data row object itself?

Roshawn
 
W

William Ryan [eMVP]

Roshawn:

I can't tell you for sure b/c I haven't really benchmarked it. However,
i've used this method on 3 production apps, one of which is pretty large and
has a large user base. I have yet to notice any performance degradation. I
think to test it, I'd need to loop through a bunch of addnews vs. adding the
datarows in order to really tell. Might be an interesting project for later
this evening ;-). Let me check it out.

HTH,

Bill
 
R

Roshawn

Hi Bill,

I now have another problem. When I press the add button to invoke the
AddNew() method of the BindingManagerBase, nothing happens. A label informs
me that a new row has indeed been added to the table's rows collection, but
the position of the BindingManagerBase doesn't move and I still can't
navigate to it.

I forgot to mention earlier that my textboxes are inside other container
controls (panels and groupboxes). Could this be the cause of the problem?

Thanks,
Roshawn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top