Values in a new datatable row

  • Thread starter Thread starter Radostin Gerchev
  • Start date Start date
R

Radostin Gerchev

Hi all,

I have a very simple question: how to set default values in a new table row?

1. DataTable.RowChanged does not work if I have gui controls bound to the
table, because it fires after the new record is accepted.
2. DataColumn.DefaultValue does not work either - what if I want to store
the exact row-creation time?
3. CurrencyManager.PositionChanged does not work either - what if I don't
have controls bound to to table.

So, where is the DataTable.OnNewRow event ???

Regards
radi
 
Usually you construct the new Row first and then add it
to the DataTable. For example, to add a row to the
Person Table you could create a function:

Public Function PersonRow(ByVal dt As DataTable)
Dim aRow As DataRow = dt.NewRow
aRow("lastname") = ""
aRow("firstname") = ""
Return aRow
End Function

and then if Person is a Table in your Dataset, ds, you
could write:

ds.Tables("Person").Rows.Add(PersonRow(ds.Tables
("Person")))

You write this code perhaps in the Click event of a
button that the user clicks to add a new row - or
something similar. Only after the Row is added can you
talk of changing the position to it.
 
You are right - but your solution works only if I create the new row
programmatically. What if the user inserts a new row through a datagrid ?

Javed said:
Usually you construct the new Row first and then add it
to the DataTable. For example, to add a row to the
Person Table you could create a function:

radi
 
Hi,
If you are using a non-typed datatable to store your new
row, you could code:

mydTable.Columns("myColumnName").DefaultValue = whatever

If you are using a strongly typed datatable, you could do
the above, OR, you could open the .xsd and click on the
datatable attribute you wish to default. In the
properties window, you will see 'default' under the Misc
properties.
Hope this helps.
JT
 
Hi,

Thank you very much for your effort.
Have you actually read my question or you just looked at the subject and
posted some random link?

I will explain again: I have a System.Data.DataTable object which may or may
not be bound to some user controls. I cannot find a way to set some default
values in a new datatable row because I cannot find an event, that fires
when a new row is created.
 
Hi,
Thanks for your answer.

Please read again my question and take a look at Scenario 2.

radi
 
Radi,
Some advice - Never preface a question with 'very simple'
and then attach three parts to it. Anyway, with regards
to scenario 2, could you not capture the time the row was
created in a datetime column in your datatable by setting
its value to Now at row creation time?
JT
 
Hi,
Some advice - Never preface a question with 'very simple'
and then attach three parts to it.

Well, the question is indeed very simple. The answer, if any, is not.
Anyway, with regards
to scenario 2, could you not capture the time the row was
created in a datetime column in your datatable by setting
its value to Now at row creation time?

I can do that. But it seems you didn't understand the problem - there is no
uniform way(event) to initialize a new DataRow in a DataTable. All of the
suggestions are bound to a particular proposition.

Anyway, thank you very much for your efforts.

radi
 
Back
Top