pb with adding columns to a datatable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sam

    pb with adding columns to a datatable

    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("S ystem.String"))
    row = dt.NewRow
    row("FieldName" ) = txtFieldName.Te xt
    dt.Rows.Add(row ) --> contains "the right string"

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

  • Stephany Young

    #2
    Re: pb with adding columns to a datatable

    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("S ystem.String"))
    dt.Columns.Add( "EntryInfo" , Type.GetType("S ystem.String"))
    dt.Columns.Add( "Whatever", Type.GetType("S ystem.String"))
    row = dt.NewRow
    row("FieldName" ) = txtFieldName.Te xt
    row("EntryInfo" ) = txtEntryInfo.Te xt
    row("Whatever") = txtWhatever.Tex t
    dt.Rows.Add(row )


    "Sam" <samuel.berthel ot@voila.fr> wrote in message
    news:1113988546 .875142.33130@o 13g2000cwo.goog legroups.com...[color=blue]
    > 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("S ystem.String"))
    > row = dt.NewRow
    > row("FieldName" ) = txtFieldName.Te xt
    > dt.Rows.Add(row ) --> contains "the right string"
    >
    > dt.Columns.Add( "EntryInfo" , Type.GetType("S ystem.String"))
    > row = dt.NewRow
    > row("EntryInfo" ) = txtEntryInfo.Te xt
    > dt.Rows.Add(row ) --> contains "" instead of "a string"
    >[/color]


    Comment

    • Sam

      #3
      Re: pb with adding columns to a datatable

      Hehe... sorry for the sms style. And my error was stupid. Thanks a lot.

      Comment

      • Cor Ligthert

        #4
        Re: pb with adding columns to a datatable

        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.T ext,txtEntryInf o.Text},true)
        ///

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

        Cor


        Comment

        • Sam

          #5
          Re: pb with adding columns to a datatable

          Thx Cor, looks good

          Comment

          Working...