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

  • Thread starter Thread starter Paulers
  • Start date Start date
P

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 :)
 
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
 
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
 
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 said:
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 said:
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
 
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 said:
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 said:
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 said:
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
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 :)
 
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 said:
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 said:
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 said:
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
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 :)
 
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 said:
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 said:
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 said:
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


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
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 :)
 
Back
Top