Shashank
Shashank

Reputation: 6337

How can I convert DataRow to string Array?

I have some values in a DataGridRow (item Array) and I want to fetch all these values into a string array. How can I achieve this?

DataGridRow row = (DataGridRow)Lst.ItemContainerGenerator.ContainerFromIndex(k);
            DataRowView Drv = (DataRowView)row.Item;
            DataRow dr = (DataRow)Drv.Row;

Upvotes: 17

Views: 80126

Answers (5)

olleh
olleh

Reputation: 2073

This one worked for me:

string[] months = string.Join(",", dataTable.Rows[0].ItemArray).Split(',').ToArray();

Upvotes: 1

Rune Grimstad
Rune Grimstad

Reputation: 36300

var rowAsString = string.Join(", ", dr.ItemArray.Select(c => c.ToString()).ToArray());

This should give you a string with each item in your data row separated by a comma.

Upvotes: 7

cyrotello
cyrotello

Reputation: 747

LINQ adds some sugar:

var stringArray = dr.ItemArray.Cast<string>().ToArray()

Upvotes: 10

Jiss
Jiss

Reputation: 321

var rowAsString = string.Join(", ", dataTable.Rows[0].ItemArray); 

No need of any lambda expresion as above.

Upvotes: 32

&#216;yvind Br&#229;then
&#216;yvind Br&#229;then

Reputation: 60694

One possibility is to call dr.ItemArray; This will get you a object[]. Then you have to cast each object to string before you use it.

Upvotes: 17

Related Questions