user2047452 user2047452 - 1 year ago 124
C# Question

How to Convert the value in DataTable into a string array in c#

I have a DataTable that contains a single row. I want to convert this DataTable values into a string array such that i can access the column values of that DataTable through the string array index
For example, if my DataTable is as follows

| Name | Address | Age |
| jim | USA | 23 |

I want to store the values in that Datatable into my string array such that MyStringArray[1] will give me the value USA.

Thanks in Advance

Answer Source

Very easy:

var stringArr = dataTable.Rows[0].ItemArray.Select(x => x.ToString()).ToArray();

Where DataRow.ItemArray property is an array of objects containing the values of the row for each columns of the data table.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download