PC Review


Reply
Thread Tools Rate Thread

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

 
 
Paulers
Guest
Posts: n/a
 
      15th Jan 2007
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

 
Reply With Quote
 
 
 
 
Stephany Young
Guest
Posts: n/a
 
      15th Jan 2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Paulers
Guest
Posts: n/a
 
      15th Jan 2007
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >


 
Reply With Quote
 
Stephany Young
Guest
Posts: n/a
 
      15th Jan 2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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
>> >

>



 
Reply With Quote
 
Paulers
Guest
Posts: n/a
 
      15th Jan 2007
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed)...
> >> > 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
> >> >

> >


 
Reply With Quote
 
Stephany Young
Guest
Posts: n/a
 
      15th Jan 2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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" <(E-Mail Removed)> wrote in message
>> >> news:(E-Mail Removed)...
>> >> > 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
>> >> >
>> >

>



 
Reply With Quote
 
Paulers
Guest
Posts: n/a
 
      15th Jan 2007
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:
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed)...
> >> > 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" <(E-Mail Removed)> wrote in message
> >> >> news:(E-Mail Removed)...
> >> >> > 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
> >> >> >
> >> >

> >


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Datatable to string array. =?Utf-8?B?cm9nZXJfMjc=?= Microsoft C# .NET 15 16th Mar 2007 07:24 AM
Error when adding array to datatable =?Utf-8?B?YQ==?= Microsoft C# .NET 2 6th May 2005 08:24 PM
Breaking up line of text > 55 chars at the last space Silvester Microsoft Access 3 6th Jul 2004 04:50 PM
Adding to a Datatable. Error: Index was outside the bounds of the array Gerry Viator Microsoft ADO .NET 2 10th Jun 2004 06:54 PM
Adding an Array of ParentColumns in a DataTable Ben Pursley Microsoft VB .NET 1 17th Sep 2003 02:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:10 PM.