BindingSource AddingNew method with DataSets

F

Flomo Togba Kwele

I am using a BindingSource whose DataSource is a typed DataSet. The data
is displayed in a DataGridView. Not all of the columns in the Datatable
are visible.

When a new row is inserted, I need to use the AddingNew method of the
BindingSource in order to place values in the hidden columns for that
newly created row.

I do not know what the 'underlying type' is for the dataset. The
following blows up at the last line because it is not. So a typed
DataRow is not the correct type. What is?

Dim row As dsFirstName.FirstNameRow = Nothing
row = DsCullFirstName.FirstName.NewFirstNameRow
row.ClientID = Tools.ClientID
row.dateAdded = Today
e.NewObject = row

Thanks for the help.
 
B

Bart Mermuys

Hi,

Flomo Togba Kwele said:
I am using a BindingSource whose DataSource is a typed DataSet. The data
is displayed in a DataGridView. Not all of the columns in the Datatable
are visible.

When a new row is inserted, I need to use the AddingNew method of the
BindingSource in order to place values in the hidden columns for that
newly created row.

I do not know what the 'underlying type' is for the dataset. The
following blows up at the last line because it is not. So a typed
DataRow is not the correct type. What is?

Dim row As dsFirstName.FirstNameRow = Nothing
row = DsCullFirstName.FirstName.NewFirstNameRow
row.ClientID = Tools.ClientID
row.dateAdded = Today
e.NewObject = row

* It's possible like this:

Private Sub FirstNameBindingSource_AddingNew(...) Handles ...
' get (pending) new DataRowView
Dim drv As DataRowView = _
DirectCast(FirstNameBindingSource.List,DataView).AddNew()

' get associated (typed) DataRow
Dim row As dsFirstName.FirstNameRow = _
DirectCast(drv.Row, dsFirstName.FirstNameRow)

' set defaults
row.ClientID = Tools.ClientID
row.dateAdded = Today
e.NewObject = drv ' note that AddNew already added the row, so the
BindingSource won't add e.NewObject again, but it must be set

' move to new record
FirstNameBindingSource.MoveLast()

End Sub


* But you might also consider using the DataTable.TableNewRow event:

Private Sub Form_Load( ..... ) handles ...
' ...
AddHandler dsFirstName.FirstName.TableNewRow, AddressOf
FirstName_TableNewRow
' ...
End Sub

Private Sub FirstName_TableNewRow( sender As Object, e As
DataTableNewRowEventArgs )

Dim row As dsFirstName.FirstNameRow = _
DirectCast(e.Row, dsFirstName.FirstNameRow)

row.ClientID = Tools.ClientID
row.dataAdded = Today

End Sub


HTH,
Greetings
 
C

Cor Ligthert [MVP]

Flomo,

A dataset exist basicly from two collections.
DataTables
DataRelations

DataTables holds
DataRows
DataColumns

DataColumns describe the items in a DataRow (they hold no data)
DataItems holds the items in a DataRow.

As soon as you start to work with typed dataset you you loose the option to
use words as nothing.

You can ask by instance
if (DataSet1.DataTable1[0].MyItemNull)
(If you use the typed dataset, you cannot do that withouth the inteligense).

Dim row As dsFirstName.FirstNameRow = Nothing
row = DsCullFirstName.FirstName.NewFirstNameRow
row.ClientID = Tools.ClientID
row.dateAdded = Today
e.NewObject = row

Taking that code it probably can be (I am not sure what you want with it)

dim row as dsFirstName.FirstnameRow = myDsFirstName.FirstName[0]
row.ClientID = Tools.ClientID
row.dateAdded = Now
DsmyFirstname.Add(row)

(All is typed by hand in this message and nothing checked it is just to give
you the ideas)

I hope this helps,

Cor
 

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