Home All Groups Group Topic Archive Search About

BindingSource AddingNew method with DataSets

Author
17 Feb 2007 12:16 AM
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.

Author
17 Feb 2007 1:59 AM
Bart Mermuys
Hi,

Show quote
"Flomo Togba Kwele" <flomo@community.nospam> wrote in message
news:MPG.203fe88ebb37a75f989680@news.covad.net...
>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


Show quote
>
> Thanks for the help.
Author
17 Feb 2007 3:36 AM
Flomo Togba Kwele
Thanks, Bart, that really helps!
Author
17 Feb 2007 6:24 AM
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






Show quote
"Flomo Togba Kwele" <flomo@community.nospam> schreef in bericht
news:MPG.203fe88ebb37a75f989680@news.covad.net...
>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.

AddThis Social Bookmark Button