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

convert a DataRow to a StringArray

P: n/a
Hello,

Is there a way to convert a DataRow to a StringArray, without looping
through all of the items of the DataRow?

Basically, I'm trying to get the results of a query and put them into
a listbox.

Right now it is done like this:

foreach(DataRow myDatarow in myDataTable)
{
String[] row = new String[myDataRow.ItemArray.Length];
for (int x = 0; x < myDataRow.ItemArray.Length; x++)
{
row[x] = myDataRow[x].ToString();
}
myListBox.Items.Add(new ListViewItem(row));

}

Unfortunately, the performance on this is poor when there is a lot of
data.

The number of columns in the DataTable depend on user input, so I
cannot simply replace all of that with

foreach(DataRow myDatarow in my DataTable)
{
myListBox.Items.Add(new ListViewItem (new String [] {
myDataRow["col1"].toString(),
myDataRow["col2"].toString(),
myDataRow["col3"].toString()
...
}

}

Any suggestions?
Thanks in advance
Nov 7 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Have you tried to virtualize the ListView? That will give you better
performance for large items.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"myotheraccount" <le*****@yahoo.comwrote in message
news:7a**********************************@u29g2000 pro.googlegroups.com...
Hello,

Is there a way to convert a DataRow to a StringArray, without looping
through all of the items of the DataRow?

Basically, I'm trying to get the results of a query and put them into
a listbox.

Right now it is done like this:

foreach(DataRow myDatarow in myDataTable)
{
String[] row = new String[myDataRow.ItemArray.Length];
for (int x = 0; x < myDataRow.ItemArray.Length; x++)
{
row[x] = myDataRow[x].ToString();
}
myListBox.Items.Add(new ListViewItem(row));

}

Unfortunately, the performance on this is poor when there is a lot of
data.

The number of columns in the DataTable depend on user input, so I
cannot simply replace all of that with

foreach(DataRow myDatarow in my DataTable)
{
myListBox.Items.Add(new ListViewItem (new String [] {
myDataRow["col1"].toString(),
myDataRow["col2"].toString(),
myDataRow["col3"].toString()
...
}

}

Any suggestions?
Thanks in advance

Nov 7 '08 #2

P: n/a
foreach (DataRow row in DataTable1.Rows)
{
object[] itemArray = row.ItemArray;
string sItem1 = itemArray[0].ToString();
}

"myotheraccount" wrote:
Hello,

Is there a way to convert a DataRow to a StringArray, without looping
through all of the items of the DataRow?

Basically, I'm trying to get the results of a query and put them into
a listbox.

Right now it is done like this:

foreach(DataRow myDatarow in myDataTable)
{
String[] row = new String[myDataRow.ItemArray.Length];
for (int x = 0; x < myDataRow.ItemArray.Length; x++)
{
row[x] = myDataRow[x].ToString();
}
myListBox.Items.Add(new ListViewItem(row));

}

Unfortunately, the performance on this is poor when there is a lot of
data.

The number of columns in the DataTable depend on user input, so I
cannot simply replace all of that with

foreach(DataRow myDatarow in my DataTable)
{
myListBox.Items.Add(new ListViewItem (new String [] {
myDataRow["col1"].toString(),
myDataRow["col2"].toString(),
myDataRow["col3"].toString()
...
}

}

Any suggestions?
Thanks in advance
Nov 7 '08 #3

P: n/a
Thanks for the suggestion. Unfortunately, I have a dynamic number of
columns. The code below only converts the first column to a string,
but I need to convert each of them to a string to load them all into
the list box, through this still forces me to loop through all of the
columns in the data row.
I'll try using a VirtualListBox.
Any more suggestions welcome.

On Nov 7, 5:18*pm, jp2msft <jp2m...@discussions.microsoft.comwrote:
foreach (DataRow row in DataTable1.Rows)
{
* object[] itemArray = row.ItemArray;
* string sItem1 = itemArray[0].ToString();

}
"myotheraccount" wrote:
Hello,
Is there a way to convert a DataRow to a StringArray, without looping
through all of the items of the DataRow?
Basically, I'm trying to get the results of a query and put them into
a listbox.
Right now it is done like this:
foreach(DataRow myDatarow in myDataTable)
{
* * String[] row = new String[myDataRow.ItemArray.Length];
* * for (int x = 0; x < myDataRow.ItemArray.Length; x++)
* * {
* * * * row[x] = myDataRow[x].ToString();
* * }
* * myListBox.Items.Add(new ListViewItem(row));
}
Unfortunately, the performance on this is poor when there is a lot of
data.
The number of columns in the DataTable depend on user input, so I
cannot simply replace all of that with
foreach(DataRow myDatarow in my DataTable)
{
* * myListBox.Items.Add(new ListViewItem (new String [] {
* * * * myDataRow["col1"].toString(),
* * * * myDataRow["col2"].toString(),
* * * * myDataRow["col3"].toString()
* * * * ...
* * * * }
}
Any suggestions?
Thanks in advance
Nov 8 '08 #4

P: n/a
My apologies for not spelling it out enough: Using the code snippet below,
the object[] itemArray will contain an array of each item in your row. If the
row has N columns, there will be an N item array.

If you still want to include Columns 1 to 3, all you need to do is index
your array accordingly.

I personally get pretty fast results from this. My code parses roughly
35,000 records of string data in about 0.3 seconds.

"myotheraccount" wrote:
Thanks for the suggestion. Unfortunately, I have a dynamic number of
columns. The code below only converts the first column to a string,
but I need to convert each of them to a string to load them all into
the list box, through this still forces me to loop through all of the
columns in the data row.
I'll try using a VirtualListBox.
Any more suggestions welcome.

On Nov 7, 5:18 pm, jp2msft <jp2m...@discussions.microsoft.comwrote:
foreach (DataRow row in DataTable1.Rows)
{
object[] itemArray = row.ItemArray;
string sItem1 = itemArray[0].ToString();

}
"myotheraccount" wrote:
Hello,
Is there a way to convert a DataRow to a StringArray, without looping
through all of the items of the DataRow?
Basically, I'm trying to get the results of a query and put them into
a listbox.
Right now it is done like this:
foreach(DataRow myDatarow in myDataTable)
{
String[] row = new String[myDataRow.ItemArray.Length];
for (int x = 0; x < myDataRow.ItemArray.Length; x++)
{
row[x] = myDataRow[x].ToString();
}
myListBox.Items.Add(new ListViewItem(row));
}
Unfortunately, the performance on this is poor when there is a lot of
data.
The number of columns in the DataTable depend on user input, so I
cannot simply replace all of that with
foreach(DataRow myDatarow in my DataTable)
{
myListBox.Items.Add(new ListViewItem (new String [] {
myDataRow["col1"].toString(),
myDataRow["col2"].toString(),
myDataRow["col3"].toString()
...
}
}
Any suggestions?
Thanks in advance

Nov 10 '08 #5

P: n/a
jp,

Thank you so much for your response. I'm a newbie to c#, so if you
could bear with me, I will really appreciate it.

What exactly do you mean by "index your array accordingly"? i.e.
Let's say my DataRow has 4 columns, what will the snippet look like
for that?
Thanks a lot.

On Nov 10, 9:09*am, jp2msft <jp2m...@discussions.microsoft.comwrote:
My apologies for not spelling it out enough: Using the code snippet below,
the object[] itemArray will contain an array of each item in your row. Ifthe
row has N columns, there will be an N item array.

If you still want to include Columns 1 to 3, all you need to do is index
your array accordingly.

I personally get pretty fast results from this. My code parses roughly
35,000 records of string data in about 0.3 seconds.

"myotheraccount" wrote:
Thanks for the suggestion. *Unfortunately, I have a dynamic number of
columns. *The code below only converts the first column to a string,
but I need to convert each of them to a string to load them all into
the list box, through this still forces me to loop through all of the
columns in the data row.
I'll try using a VirtualListBox.
Any more suggestions welcome.
On Nov 7, 5:18 pm, jp2msft <jp2m...@discussions.microsoft.comwrote:
foreach (DataRow row in DataTable1.Rows)
{
* object[] itemArray = row.ItemArray;
* string sItem1 = itemArray[0].ToString();
}
"myotheraccount" wrote:
Hello,
Is there a way to convert a DataRow to a StringArray, without looping
through all of the items of the DataRow?
Basically, I'm trying to get the results of a query and put them into
a listbox.
Right now it is done like this:
foreach(DataRow myDatarow in myDataTable)
{
* * String[] row = new String[myDataRow.ItemArray.Length];
* * for (int x = 0; x < myDataRow.ItemArray.Length; x++)
* * {
* * * * row[x] = myDataRow[x].ToString();
* * }
* * myListBox.Items.Add(new ListViewItem(row));
}
Unfortunately, the performance on this is poor when there is a lot of
data.
The number of columns in the DataTable depend on user input, so I
cannot simply replace all of that with
foreach(DataRow myDatarow in my DataTable)
{
* * myListBox.Items.Add(new ListViewItem (new String [] {
* * * * myDataRow["col1"].toString(),
* * * * myDataRow["col2"].toString(),
* * * * myDataRow["col3"].toString()
* * * * ...
* * * * }
}
Any suggestions?
Thanks in advance
Nov 10 '08 #6

P: n/a
With 4 columns, you would do something like this:

foreach (DataRow row in DataTable1.Rows)
{
object[] itemArray = row.ItemArray;
// this is a "checking" step
if (itemArray.Length == 4) {
string sItem1 = itemArray[0].ToString();
string sItem2 = itemArray[1].ToString();
string sItem3 = itemArray[2].ToString();
string sItem4 = itemArray[3].ToString();
// or, if you wanted it in a list view:
string sLvItem = itemArray[0].ToString() + ";"
+ itemArray[1].ToString() + ";"
+ itemArray[2].ToString() + ";"
+ itemArray[3].ToString() + ";"
ListView1.Items.Add(sLvItem);
}
}

Of course, I'm not real sure what your code is trying to do, but this should
give you an idea how to access the elements in your row.

"myotheraccount" wrote:
jp,

Thank you so much for your response. I'm a newbie to c#, so if you
could bear with me, I will really appreciate it.

What exactly do you mean by "index your array accordingly"? i.e.
Let's say my DataRow has 4 columns, what will the snippet look like
for that?
Thanks a lot.

On Nov 10, 9:09 am, jp2msft <jp2m...@discussions.microsoft.comwrote:
My apologies for not spelling it out enough: Using the code snippet below,
the object[] itemArray will contain an array of each item in your row. If the
row has N columns, there will be an N item array.

If you still want to include Columns 1 to 3, all you need to do is index
your array accordingly.

I personally get pretty fast results from this. My code parses roughly
35,000 records of string data in about 0.3 seconds.

"myotheraccount" wrote:
Thanks for the suggestion. Unfortunately, I have a dynamic number of
columns. The code below only converts the first column to a string,
but I need to convert each of them to a string to load them all into
the list box, through this still forces me to loop through all of the
columns in the data row.
I'll try using a VirtualListBox.
Any more suggestions welcome.
On Nov 7, 5:18 pm, jp2msft <jp2m...@discussions.microsoft.comwrote:
foreach (DataRow row in DataTable1.Rows)
{
object[] itemArray = row.ItemArray;
string sItem1 = itemArray[0].ToString();
}
"myotheraccount" wrote:
Hello,
Is there a way to convert a DataRow to a StringArray, without looping
through all of the items of the DataRow?
Basically, I'm trying to get the results of a query and put them into
a listbox.
Right now it is done like this:
foreach(DataRow myDatarow in myDataTable)
{
String[] row = new String[myDataRow.ItemArray.Length];
for (int x = 0; x < myDataRow.ItemArray.Length; x++)
{
row[x] = myDataRow[x].ToString();
}
myListBox.Items.Add(new ListViewItem(row));
}
Unfortunately, the performance on this is poor when there is a lot of
data.
The number of columns in the DataTable depend on user input, so I
cannot simply replace all of that with
foreach(DataRow myDatarow in my DataTable)
{
myListBox.Items.Add(new ListViewItem (new String [] {
myDataRow["col1"].toString(),
myDataRow["col2"].toString(),
myDataRow["col3"].toString()
...
}
}
Any suggestions?
Thanks in advance

Nov 10 '08 #7

P: n/a
Is this any different that in my original example:

foreach(DataRow myDatarow in myDataTable)
{
String[] row = new String[myDataRow.ItemArray.Length];
for (int x = 0; x < myDataRow.ItemArray.Length; x++)
{
row[x] = myDataRow[x].ToString();
}
myListBox.Items.Add(new ListViewItem(row));

}

It looks like the only difference is that my way coverts the elements
of the DataRow directly, instead of copying them to an object array
first.
What I'm trying to avoid is having to convert each column
individually. I could have as many as 35 or 40 columns. Looping
through 40 columns for each of, say, 10,000 rows appears to be quite
time consuming. I wanted to know if there was a way to convert the
entire array in one shot?
Thanks

On Nov 10, 11:53*am, jp2msft <jp2m...@discussions.microsoft.com>
wrote:
With 4 columns, you would do something like this:

foreach (DataRow row in DataTable1.Rows)
{
* object[] itemArray = row.ItemArray;
* // this is a "checking" step
* if (itemArray.Length == 4) {
* * string sItem1 = itemArray[0].ToString();
* * string sItem2 = itemArray[1].ToString();
* * string sItem3 = itemArray[2].ToString();
* * string sItem4 = itemArray[3].ToString();
* * // or, if you wanted it in a list view:
* * string sLvItem = itemArray[0].ToString() + ";"
* * * + itemArray[1].ToString() + ";"
* * * + itemArray[2].ToString() + ";"
* * * + itemArray[3].ToString() + ";"
* * ListView1.Items.Add(sLvItem);
* }

}

Of course, I'm not real sure what your code is trying to do, but this should
give you an idea how to access the elements in your row.

"myotheraccount" wrote:
jp,
Thank you so much for your response. *I'm a newbie to c#, so if you
could bear with me, I will really appreciate it.
What exactly do you mean by "index your array accordingly"? *i.e.
Let's say my DataRow has 4 columns, what will the snippet look like
for that?
Thanks a lot.
On Nov 10, 9:09 am, jp2msft <jp2m...@discussions.microsoft.comwrote:
My apologies for not spelling it out enough: Using the code snippet below,
the object[] itemArray will contain an array of each item in your row.. If the
row has N columns, there will be an N item array.
If you still want to include Columns 1 to 3, all you need to do is index
your array accordingly.
I personally get pretty fast results from this. My code parses roughly
35,000 records of string data in about 0.3 seconds.
"myotheraccount" wrote:
Thanks for the suggestion. *Unfortunately, I have a dynamic number of
columns. *The code below only converts the first column to a string,
but I need to convert each of them to a string to load them all into
the list box, through this still forces me to loop through all of the
columns in the data row.
I'll try using a VirtualListBox.
Any more suggestions welcome.
On Nov 7, 5:18 pm, jp2msft <jp2m...@discussions.microsoft.comwrote:
foreach (DataRow row in DataTable1.Rows)
{
* object[] itemArray = row.ItemArray;
* string sItem1 = itemArray[0].ToString();
}
"myotheraccount" wrote:
Hello,
Is there a way to convert a DataRow to a StringArray, without looping
through all of the items of the DataRow?
Basically, I'm trying to get the results of a query and put them into
a listbox.
Right now it is done like this:
foreach(DataRow myDatarow in myDataTable)
{
* * String[] row = new String[myDataRow.ItemArray.Length];
* * for (int x = 0; x < myDataRow.ItemArray.Length; x++)
* * {
* * * * row[x] = myDataRow[x].ToString();
* * }
* * myListBox.Items.Add(new ListViewItem(row));
}
Unfortunately, the performance on this is poor when there is a lot of
data.
The number of columns in the DataTable depend on user input, soI
cannot simply replace all of that with
foreach(DataRow myDatarow in my DataTable)
{
* * myListBox.Items.Add(new ListViewItem (new String [] {
* * * * myDataRow["col1"].toString(),
* * * * myDataRow["col2"].toString(),
* * * * myDataRow["col3"].toString()
* * * * ...
* * * * }
}
Any suggestions?
Thanks in advance
Nov 10 '08 #8

P: n/a
Is this any different that in my original example:

foreach(DataRow myDatarow in myDataTable)
{
String[] row = new String[myDataRow.ItemArray.Length];
for (int x = 0; x < myDataRow.ItemArray.Length; x++)
{
row[x] = myDataRow[x].ToString();
}
myListBox.Items.Add(new ListViewItem(row));

}

It looks like the only difference is that my way coverts the elements
of the DataRow directly, instead of copying them to an object array
first.
What I'm trying to avoid is having to convert each column
individually. I could have as many as 35 or 40 columns. Looping
through 40 columns for each of, say, 10,000 rows appears to be quite
time consuming. I wanted to know if there was a way to convert the
entire array in one shot?
Thanks
On Nov 10, 11:53*am, jp2msft <jp2m...@discussions.microsoft.com>
wrote:
With 4 columns, you would do something like this:

foreach (DataRow row in DataTable1.Rows)
{
* object[] itemArray = row.ItemArray;
* // this is a "checking" step
* if (itemArray.Length == 4) {
* * string sItem1 = itemArray[0].ToString();
* * string sItem2 = itemArray[1].ToString();
* * string sItem3 = itemArray[2].ToString();
* * string sItem4 = itemArray[3].ToString();
* * // or, if you wanted it in a list view:
* * string sLvItem = itemArray[0].ToString() + ";"
* * * + itemArray[1].ToString() + ";"
* * * + itemArray[2].ToString() + ";"
* * * + itemArray[3].ToString() + ";"
* * ListView1.Items.Add(sLvItem);
* }

}

Of course, I'm not real sure what your code is trying to do, but this should
give you an idea how to access the elements in your row.

"myotheraccount" wrote:
jp,
Thank you so much for your response. *I'm a newbie to c#, so if you
could bear with me, I will really appreciate it.
What exactly do you mean by "index your array accordingly"? *i.e.
Let's say my DataRow has 4 columns, what will the snippet look like
for that?
Thanks a lot.
On Nov 10, 9:09 am, jp2msft <jp2m...@discussions.microsoft.comwrote:
My apologies for not spelling it out enough: Using the code snippet below,
the object[] itemArray will contain an array of each item in your row.. If the
row has N columns, there will be an N item array.
If you still want to include Columns 1 to 3, all you need to do is index
your array accordingly.
I personally get pretty fast results from this. My code parses roughly
35,000 records of string data in about 0.3 seconds.
"myotheraccount" wrote:
Thanks for the suggestion. *Unfortunately, I have a dynamic number of
columns. *The code below only converts the first column to a string,
but I need to convert each of them to a string to load them all into
the list box, through this still forces me to loop through all of the
columns in the data row.
I'll try using a VirtualListBox.
Any more suggestions welcome.
On Nov 7, 5:18 pm, jp2msft <jp2m...@discussions.microsoft.comwrote:
foreach (DataRow row in DataTable1.Rows)
{
* object[] itemArray = row.ItemArray;
* string sItem1 = itemArray[0].ToString();
}
"myotheraccount" wrote:
Hello,
Is there a way to convert a DataRow to a StringArray, without looping
through all of the items of the DataRow?
Basically, I'm trying to get the results of a query and put them into
a listbox.
Right now it is done like this:
foreach(DataRow myDatarow in myDataTable)
{
* * String[] row = new String[myDataRow.ItemArray.Length];
* * for (int x = 0; x < myDataRow.ItemArray.Length; x++)
* * {
* * * * row[x] = myDataRow[x].ToString();
* * }
* * myListBox.Items.Add(new ListViewItem(row));
}
Unfortunately, the performance on this is poor when there is a lot of
data.
The number of columns in the DataTable depend on user input, soI
cannot simply replace all of that with
foreach(DataRow myDatarow in my DataTable)
{
* * myListBox.Items.Add(new ListViewItem (new String [] {
* * * * myDataRow["col1"].toString(),
* * * * myDataRow["col2"].toString(),
* * * * myDataRow["col3"].toString()
* * * * ...
* * * * }
}
Any suggestions?
Thanks in advance
Nov 10 '08 #9

P: n/a
Again, I misunderstood. I thought you wanted a way to build your array faster
in fewer steps.

If you are looking to have less data to dig through, try something like this
(modified to how you need to use it, of course):

Is this any different that in my original example:

foreach(DataRow myDatarow in myDataTable.Select("JobTitle=MANAGER"))
{
myListBox.Items.Add(new ListViewItem(myDatarow["FullName"]));
}

Is this more in line with what you are looking for? Without more
information, I don't know how to help you further.

~Joe
"myotheraccount" wrote:
Is this any different that in my original example:

foreach(DataRow myDatarow in myDataTable)
{
String[] row = new String[myDataRow.ItemArray.Length];
for (int x = 0; x < myDataRow.ItemArray.Length; x++)
{
row[x] = myDataRow[x].ToString();
}
myListBox.Items.Add(new ListViewItem(row));

}

It looks like the only difference is that my way coverts the elements
of the DataRow directly, instead of copying them to an object array
first.
What I'm trying to avoid is having to convert each column
individually. I could have as many as 35 or 40 columns. Looping
through 40 columns for each of, say, 10,000 rows appears to be quite
time consuming. I wanted to know if there was a way to convert the
entire array in one shot?
Thanks
On Nov 10, 11:53 am, jp2msft <jp2m...@discussions.microsoft.com>
wrote:
With 4 columns, you would do something like this:

foreach (DataRow row in DataTable1.Rows)
{
object[] itemArray = row.ItemArray;
// this is a "checking" step
if (itemArray.Length == 4) {
string sItem1 = itemArray[0].ToString();
string sItem2 = itemArray[1].ToString();
string sItem3 = itemArray[2].ToString();
string sItem4 = itemArray[3].ToString();
// or, if you wanted it in a list view:
string sLvItem = itemArray[0].ToString() + ";"
+ itemArray[1].ToString() + ";"
+ itemArray[2].ToString() + ";"
+ itemArray[3].ToString() + ";"
ListView1.Items.Add(sLvItem);
}

}

Of course, I'm not real sure what your code is trying to do, but this should
give you an idea how to access the elements in your row.

"myotheraccount" wrote:
jp,
Thank you so much for your response. I'm a newbie to c#, so if you
could bear with me, I will really appreciate it.
What exactly do you mean by "index your array accordingly"? i.e.
Let's say my DataRow has 4 columns, what will the snippet look like
for that?
Thanks a lot.
On Nov 10, 9:09 am, jp2msft <jp2m...@discussions.microsoft.comwrote:
My apologies for not spelling it out enough: Using the code snippet below,
the object[] itemArray will contain an array of each item in your row.. If the
row has N columns, there will be an N item array.
If you still want to include Columns 1 to 3, all you need to do is index
your array accordingly.
I personally get pretty fast results from this. My code parses roughly
35,000 records of string data in about 0.3 seconds.
"myotheraccount" wrote:
Thanks for the suggestion. Unfortunately, I have a dynamic number of
columns. The code below only converts the first column to a string,
but I need to convert each of them to a string to load them all into
the list box, through this still forces me to loop through all of the
columns in the data row.
I'll try using a VirtualListBox.
Any more suggestions welcome.
On Nov 7, 5:18 pm, jp2msft <jp2m...@discussions.microsoft.comwrote:
foreach (DataRow row in DataTable1.Rows)
{
object[] itemArray = row.ItemArray;
string sItem1 = itemArray[0].ToString();
}
"myotheraccount" wrote:
Hello,
Is there a way to convert a DataRow to a StringArray, without looping
through all of the items of the DataRow?
Basically, I'm trying to get the results of a query and put them into
a listbox.
Right now it is done like this:
foreach(DataRow myDatarow in myDataTable)
{
String[] row = new String[myDataRow.ItemArray.Length];
for (int x = 0; x < myDataRow.ItemArray.Length; x++)
{
row[x] = myDataRow[x].ToString();
}
myListBox.Items.Add(new ListViewItem(row));
}
Unfortunately, the performance on this is poor when there is a lot of
data.
The number of columns in the DataTable depend on user input, so I
cannot simply replace all of that with
foreach(DataRow myDatarow in my DataTable)
{
myListBox.Items.Add(new ListViewItem (new String [] {
myDataRow["col1"].toString(),
myDataRow["col2"].toString(),
myDataRow["col3"].toString()
...
}
}
Any suggestions?
Thanks in advance

Nov 10 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.