17

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;
Manoj Govindan
  • 72,339
  • 21
  • 134
  • 141
Shashank
  • 6,117
  • 20
  • 51
  • 72

5 Answers5

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

No need of any lambda expresion as above.

Xharze
  • 2,703
  • 2
  • 17
  • 30
Jiss
  • 321
  • 1
  • 3
  • 3
17

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.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
10

LINQ adds some sugar:

var stringArray = dr.ItemArray.Cast<string>().ToArray()
cyrotello
  • 757
  • 9
  • 26
7
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.

Rune Grimstad
  • 35,612
  • 10
  • 61
  • 76
1

This one worked for me:

string[] months = string.Join(",", dataTable.Rows[0].ItemArray).Split(',').ToArray();
olleh
  • 1,915
  • 18
  • 21