Home All Groups Group Topic Archive Search About

breaking up a String into an array of chars and adding to datatable

Author
14 Jan 2007 11:38 PM
Paulers
Hello,

I have a string that I am trying to add each char to a datatable row.

for example if I have a string that looks like "abcdefg", I would like
to break it up into an array of characters so I can do this:

myDataTable.Rows.Add(array())

instead of myDataTable.Rows.Add("a","b","c","d","e","f","g")

You help is greatly appreciated!

thanks :)

Author
14 Jan 2007 11:56 PM
Stephany Young
You need to convert the string to an array of characters:

  Dim chararray As Char() = MyString.ToCharArray()

and then feed the array of characters to the Rows.Add method:

  myDataTable.Rows.Add(chararray)

If you do not need to 'save' the interim result for another purpose then you
could feed it in directly:

  myDataTable.Rows.Add(MyString.ToCharArray())



character
Show quoteHide quote
"Paulers" <SuperG***@gmail.com> wrote in message
news:1168817895.376215.280060@s34g2000cwa.googlegroups.com...
> Hello,
>
> I have a string that I am trying to add each char to a datatable row.
>
> for example if I have a string that looks like "abcdefg", I would like
> to break it up into an array of characters so I can do this:
>
> myDataTable.Rows.Add(array())
>
> instead of myDataTable.Rows.Add("a","b","c","d","e","f","g")
>
> You help is greatly appreciated!
>
> thanks :)
>
Author
15 Jan 2007 12:26 AM
Paulers
Hello and thanks for your response!

I gave that a try and when I display my DataGridView it shows
System.Char[] in the first col.

Here is what I did

        Dim d As DataTable = New DataTable
        Dim line As String = "abcdefg"
        d.Rows.Add(line.ToCharArray())
        Me.DataGridView1.DataSource = d

Stephany Young wrote:
Show quoteHide quote
> You need to convert the string to an array of characters:
>
>   Dim chararray As Char() = MyString.ToCharArray()
>
> and then feed the array of characters to the Rows.Add method:
>
>   myDataTable.Rows.Add(chararray)
>
> If you do not need to 'save' the interim result for another purpose then you
> could feed it in directly:
>
>   myDataTable.Rows.Add(MyString.ToCharArray())
>
>
>
> character
> "Paulers" <SuperG***@gmail.com> wrote in message
> news:1168817895.376215.280060@s34g2000cwa.googlegroups.com...
> > Hello,
> >
> > I have a string that I am trying to add each char to a datatable row.
> >
> > for example if I have a string that looks like "abcdefg", I would like
> > to break it up into an array of characters so I can do this:
> >
> > myDataTable.Rows.Add(array())
> >
> > instead of myDataTable.Rows.Add("a","b","c","d","e","f","g")
> >
> > You help is greatly appreciated!
> >
> > thanks :)
> >
Author
15 Jan 2007 1:11 AM
Stephany Young
Sorry, you can't do this directly with the Rows.Add method.

You have to do it indirectly, something like:

  Dim d As DataTable = New DataTable
  Dim line As String = "abcdefg"
  For i As Integer = 0 to line.Length - 1
    d.Columns.Add(New DataColumn("c" & i.ToString,
Type.GetType("System.Char")))
  Next
  Dim dr As DataRow = _d.NewRow()
  dr.ItemArray = line.ToCharArray()
  d.Rows.Add(_dr)
  Me.DataGridView1.DataSource = d


Show quoteHide quote
"Paulers" <SuperG***@gmail.com> wrote in message
news:1168820805.773667.51220@l53g2000cwa.googlegroups.com...
> Hello and thanks for your response!
>
> I gave that a try and when I display my DataGridView it shows
> System.Char[] in the first col.
>
> Here is what I did
>
>        Dim d As DataTable = New DataTable
>        Dim line As String = "abcdefg"
>        d.Rows.Add(line.ToCharArray())
>        Me.DataGridView1.DataSource = d
>
> Stephany Young wrote:
>> You need to convert the string to an array of characters:
>>
>>   Dim chararray As Char() = MyString.ToCharArray()
>>
>> and then feed the array of characters to the Rows.Add method:
>>
>>   myDataTable.Rows.Add(chararray)
>>
>> If you do not need to 'save' the interim result for another purpose then
>> you
>> could feed it in directly:
>>
>>   myDataTable.Rows.Add(MyString.ToCharArray())
>>
>>
>>
>> character
>> "Paulers" <SuperG***@gmail.com> wrote in message
>> news:1168817895.376215.280060@s34g2000cwa.googlegroups.com...
>> > Hello,
>> >
>> > I have a string that I am trying to add each char to a datatable row.
>> >
>> > for example if I have a string that looks like "abcdefg", I would like
>> > to break it up into an array of characters so I can do this:
>> >
>> > myDataTable.Rows.Add(array())
>> >
>> > instead of myDataTable.Rows.Add("a","b","c","d","e","f","g")
>> >
>> > You help is greatly appreciated!
>> >
>> > thanks :)
>> >
>
Author
15 Jan 2007 1:25 AM
Paulers
Thank you Stephany!

I tried what you suggested and now on the line containing:

dr.ItemArray = line.ToCharArray()

I get an error messahe stating "Error    1    Value of type '1-dimensional
array of Char' cannot be converted to '1-dimensional array of Object'
because 'Char' is not a reference type."


Stephany Young wrote:
Show quoteHide quote
> Sorry, you can't do this directly with the Rows.Add method.
>
> You have to do it indirectly, something like:
>
>   Dim d As DataTable = New DataTable
>   Dim line As String = "abcdefg"
>   For i As Integer = 0 to line.Length - 1
>     d.Columns.Add(New DataColumn("c" & i.ToString,
> Type.GetType("System.Char")))
>   Next
>   Dim dr As DataRow = _d.NewRow()
>   dr.ItemArray = line.ToCharArray()
>   d.Rows.Add(_dr)
>   Me.DataGridView1.DataSource = d
>
>
> "Paulers" <SuperG***@gmail.com> wrote in message
> news:1168820805.773667.51220@l53g2000cwa.googlegroups.com...
> > Hello and thanks for your response!
> >
> > I gave that a try and when I display my DataGridView it shows
> > System.Char[] in the first col.
> >
> > Here is what I did
> >
> >        Dim d As DataTable = New DataTable
> >        Dim line As String = "abcdefg"
> >        d.Rows.Add(line.ToCharArray())
> >        Me.DataGridView1.DataSource = d
> >
> > Stephany Young wrote:
> >> You need to convert the string to an array of characters:
> >>
> >>   Dim chararray As Char() = MyString.ToCharArray()
> >>
> >> and then feed the array of characters to the Rows.Add method:
> >>
> >>   myDataTable.Rows.Add(chararray)
> >>
> >> If you do not need to 'save' the interim result for another purpose then
> >> you
> >> could feed it in directly:
> >>
> >>   myDataTable.Rows.Add(MyString.ToCharArray())
> >>
> >>
> >>
> >> character
> >> "Paulers" <SuperG***@gmail.com> wrote in message
> >> news:1168817895.376215.280060@s34g2000cwa.googlegroups.com...
> >> > Hello,
> >> >
> >> > I have a string that I am trying to add each char to a datatable row.
> >> >
> >> > for example if I have a string that looks like "abcdefg", I would like
> >> > to break it up into an array of characters so I can do this:
> >> >
> >> > myDataTable.Rows.Add(array())
> >> >
> >> > instead of myDataTable.Rows.Add("a","b","c","d","e","f","g")
> >> >
> >> > You help is greatly appreciated!
> >> >
> >> > thanks :)
> >> >
> >
Author
15 Jan 2007 1:42 AM
Stephany Young
Well you have a couple of options:

  Dim d As DataTable = New DataTable
  Dim line As String = "abcdefg"
  For i As Integer = 0 to line.Length - 1
    d.Columns.Add(New DataColumn("c" & i.ToString,
Type.GetType("System.Char")))
  Next
  Dim dr As DataRow = _d.NewRow()
  Dim o As Object() = new Object(line.Length -1) {}
  For i As Integer = 0 to line.Length - 1
    o(_i) = line.Chars(i)
  Next
  dr.ItemArray = o
  d.Rows.Add(_dr)
  Me.DataGridView1.DataSource = d

or:

  Dim d As DataTable = New DataTable
  Dim line As String = "abcdefg"
  For i As Integer = 0 to line.Length - 1
    d.Columns.Add(New DataColumn("c" & i.ToString,
Type.GetType("System.Char")))
  Next
  Dim dr As DataRow = _d.NewRow()
  For i As Integer = 0 to line.Length - 1
    dr(_i) = lone.Chars(i)
  Next
  d.Rows.Add(_dr)
  Me.DataGridView1.DataSource = d

Either way, you need to deal with each character individually.


Show quoteHide quote
"Paulers" <SuperG***@gmail.com> wrote in message
news:1168824336.593034.126530@q2g2000cwa.googlegroups.com...
> Thank you Stephany!
>
> I tried what you suggested and now on the line containing:
>
> dr.ItemArray = line.ToCharArray()
>
> I get an error messahe stating "Error 1 Value of type '1-dimensional
> array of Char' cannot be converted to '1-dimensional array of Object'
> because 'Char' is not a reference type."
>
>
> Stephany Young wrote:
>> Sorry, you can't do this directly with the Rows.Add method.
>>
>> You have to do it indirectly, something like:
>>
>>   Dim d As DataTable = New DataTable
>>   Dim line As String = "abcdefg"
>>   For i As Integer = 0 to line.Length - 1
>>     d.Columns.Add(New DataColumn("c" & i.ToString,
>> Type.GetType("System.Char")))
>>   Next
>>   Dim dr As DataRow = _d.NewRow()
>>   dr.ItemArray = line.ToCharArray()
>>   d.Rows.Add(_dr)
>>   Me.DataGridView1.DataSource = d
>>
>>
>> "Paulers" <SuperG***@gmail.com> wrote in message
>> news:1168820805.773667.51220@l53g2000cwa.googlegroups.com...
>> > Hello and thanks for your response!
>> >
>> > I gave that a try and when I display my DataGridView it shows
>> > System.Char[] in the first col.
>> >
>> > Here is what I did
>> >
>> >        Dim d As DataTable = New DataTable
>> >        Dim line As String = "abcdefg"
>> >        d.Rows.Add(line.ToCharArray())
>> >        Me.DataGridView1.DataSource = d
>> >
>> > Stephany Young wrote:
>> >> You need to convert the string to an array of characters:
>> >>
>> >>   Dim chararray As Char() = MyString.ToCharArray()
>> >>
>> >> and then feed the array of characters to the Rows.Add method:
>> >>
>> >>   myDataTable.Rows.Add(chararray)
>> >>
>> >> If you do not need to 'save' the interim result for another purpose
>> >> then
>> >> you
>> >> could feed it in directly:
>> >>
>> >>   myDataTable.Rows.Add(MyString.ToCharArray())
>> >>
>> >>
>> >>
>> >> character
>> >> "Paulers" <SuperG***@gmail.com> wrote in message
>> >> news:1168817895.376215.280060@s34g2000cwa.googlegroups.com...
>> >> > Hello,
>> >> >
>> >> > I have a string that I am trying to add each char to a datatable
>> >> > row.
>> >> >
>> >> > for example if I have a string that looks like "abcdefg", I would
>> >> > like
>> >> > to break it up into an array of characters so I can do this:
>> >> >
>> >> > myDataTable.Rows.Add(array())
>> >> >
>> >> > instead of myDataTable.Rows.Add("a","b","c","d","e","f","g")
>> >> >
>> >> > You help is greatly appreciated!
>> >> >
>> >> > thanks :)
>> >> >
>> >
>
Author
15 Jan 2007 2:31 AM
Paulers
Stephany,

You are great! Thanks a million for your help. I hope some day I am as
knowledgable about vb.net as you are.


Stephany Young wrote:
Show quoteHide quote
> Well you have a couple of options:
>
>   Dim d As DataTable = New DataTable
>   Dim line As String = "abcdefg"
>   For i As Integer = 0 to line.Length - 1
>     d.Columns.Add(New DataColumn("c" & i.ToString,
> Type.GetType("System.Char")))
>   Next
>   Dim dr As DataRow = _d.NewRow()
>   Dim o As Object() = new Object(line.Length -1) {}
>   For i As Integer = 0 to line.Length - 1
>     o(_i) = line.Chars(i)
>   Next
>   dr.ItemArray = o
>   d.Rows.Add(_dr)
>   Me.DataGridView1.DataSource = d
>
> or:
>
>   Dim d As DataTable = New DataTable
>   Dim line As String = "abcdefg"
>   For i As Integer = 0 to line.Length - 1
>     d.Columns.Add(New DataColumn("c" & i.ToString,
> Type.GetType("System.Char")))
>   Next
>   Dim dr As DataRow = _d.NewRow()
>   For i As Integer = 0 to line.Length - 1
>     dr(_i) = lone.Chars(i)
>   Next
>   d.Rows.Add(_dr)
>   Me.DataGridView1.DataSource = d
>
> Either way, you need to deal with each character individually.
>
>
> "Paulers" <SuperG***@gmail.com> wrote in message
> news:1168824336.593034.126530@q2g2000cwa.googlegroups.com...
> > Thank you Stephany!
> >
> > I tried what you suggested and now on the line containing:
> >
> > dr.ItemArray = line.ToCharArray()
> >
> > I get an error messahe stating "Error 1 Value of type '1-dimensional
> > array of Char' cannot be converted to '1-dimensional array of Object'
> > because 'Char' is not a reference type."
> >
> >
> > Stephany Young wrote:
> >> Sorry, you can't do this directly with the Rows.Add method.
> >>
> >> You have to do it indirectly, something like:
> >>
> >>   Dim d As DataTable = New DataTable
> >>   Dim line As String = "abcdefg"
> >>   For i As Integer = 0 to line.Length - 1
> >>     d.Columns.Add(New DataColumn("c" & i.ToString,
> >> Type.GetType("System.Char")))
> >>   Next
> >>   Dim dr As DataRow = _d.NewRow()
> >>   dr.ItemArray = line.ToCharArray()
> >>   d.Rows.Add(_dr)
> >>   Me.DataGridView1.DataSource = d
> >>
> >>
> >> "Paulers" <SuperG***@gmail.com> wrote in message
> >> news:1168820805.773667.51220@l53g2000cwa.googlegroups.com...
> >> > Hello and thanks for your response!
> >> >
> >> > I gave that a try and when I display my DataGridView it shows
> >> > System.Char[] in the first col.
> >> >
> >> > Here is what I did
> >> >
> >> >        Dim d As DataTable = New DataTable
> >> >        Dim line As String = "abcdefg"
> >> >        d.Rows.Add(line.ToCharArray())
> >> >        Me.DataGridView1.DataSource = d
> >> >
> >> > Stephany Young wrote:
> >> >> You need to convert the string to an array of characters:
> >> >>
> >> >>   Dim chararray As Char() = MyString.ToCharArray()
> >> >>
> >> >> and then feed the array of characters to the Rows.Add method:
> >> >>
> >> >>   myDataTable.Rows.Add(chararray)
> >> >>
> >> >> If you do not need to 'save' the interim result for another purpose
> >> >> then
> >> >> you
> >> >> could feed it in directly:
> >> >>
> >> >>   myDataTable.Rows.Add(MyString.ToCharArray())
> >> >>
> >> >>
> >> >>
> >> >> character
> >> >> "Paulers" <SuperG***@gmail.com> wrote in message
> >> >> news:1168817895.376215.280060@s34g2000cwa.googlegroups.com...
> >> >> > Hello,
> >> >> >
> >> >> > I have a string that I am trying to add each char to a datatable
> >> >> > row.
> >> >> >
> >> >> > for example if I have a string that looks like "abcdefg", I would
> >> >> > like
> >> >> > to break it up into an array of characters so I can do this:
> >> >> >
> >> >> > myDataTable.Rows.Add(array())
> >> >> >
> >> >> > instead of myDataTable.Rows.Add("a","b","c","d","e","f","g")
> >> >> >
> >> >> > You help is greatly appreciated!
> >> >> >
> >> >> > thanks :)
> >> >> >
> >> >
> >