pb with adding columns to a datatable

S

Sam

Hi,
I'm adding columns to a datatable as followed. The values are from
textboxes or comboboxes.
The first column is properly field but then all the subsequent columns
just contain "", whereas the source txtboxes or comboboxes values are
correct. Why is that ?

thx

Dim dt As New DataTable
Dim row As DataRow

dt.Columns.Add("FieldName", Type.GetType("System.String"))
row = dt.NewRow
row("FieldName") = txtFieldName.Text
dt.Rows.Add(row) --> contains "the right string"

dt.Columns.Add("EntryInfo", Type.GetType("System.String"))
row = dt.NewRow
row("EntryInfo") = txtEntryInfo.Text
dt.Rows.Add(row) --> contains "" instead of "a string"
 
S

Stephany Young

First of all - pb is the chemical symbol for lead and this is not an SMS
newsgroup so there is no need to abbreviate things.

The code you have shown will give you a table that looks like this:

FieldName EntryInfo Whatever
--------- --------- --------
Row 0 aaa
Row 1 bbb
Row 2 ccc

I assume that this is probably not what you want.

If, instead, you want this:

FieldName EntryInfo Whatever
--------- --------- --------
Row 0 aaa bbb ccc

then you need:

Dim dt As New DataTable
Dim row As DataRow
dt.Columns.Add("FieldName", Type.GetType("System.String"))
dt.Columns.Add("EntryInfo", Type.GetType("System.String"))
dt.Columns.Add("Whatever", Type.GetType("System.String"))
row = dt.NewRow
row("FieldName") = txtFieldName.Text
row("EntryInfo") = txtEntryInfo.Text
row("Whatever") = txtWhatever.Text
dt.Rows.Add(row)
 
C

Cor Ligthert

Sam,

I would change it in this typed in this message so watch typos..

\\\
Dim dt As New DataTable
dt.Columns.Add("FieldName", GetType(System.String))
dt.Columns.Add("EntryInfo", GetType(System.String))
dt.loaddatarow(new Object() {txtFieldName.Text,txtEntryInfo.Text},true)
///

A lot shorter and the rowstate is direct set to unchanged in this case
(otherwise false).

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