By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,877 Members | 1,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,877 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 :)

Jan 14 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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" <Su*******@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.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 :)

Jan 15 '07 #2

P: n/a
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" <Su*******@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.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 :)
Jan 15 '07 #3

P: n/a
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" <Su*******@gmail.comwrote in message
news:11*********************@l53g2000cwa.googlegro ups.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" <Su*******@gmail.comwrote in message
news:11**********************@s34g2000cwa.googleg roups.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 :)

Jan 15 '07 #4

P: n/a
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" <Su*******@gmail.comwrote in message
news:11*********************@l53g2000cwa.googlegro ups.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" <Su*******@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.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 :)
Jan 15 '07 #5

P: n/a
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" <Su*******@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.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" <Su*******@gmail.comwrote in message
news:11*********************@l53g2000cwa.googlegr oups.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" <Su*******@gmail.comwrote in message
news:11**********************@s34g2000cwa.googleg roups.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 :)


Jan 15 '07 #6

P: n/a
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" <Su*******@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.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" <Su*******@gmail.comwrote in message
news:11*********************@l53g2000cwa.googlegro ups.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" <Su*******@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.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 :)
Jan 15 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.