Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert DataRow to string Array?

Tags:

c#

datarow

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;
like image 847
Shashank Avatar asked Sep 20 '10 06:09

Shashank


People also ask

How to convert DataRow to string array in c#?

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

How to convert DataTable column to string array in c#?

Select(row => row["columnName"]. ToString()). ToArray();

How to convert DataTable to array in c# using linq?

Convert DataTable to Custom class ArrayAsEnumerable(). Select(item => new { fullName = string. Format("{0}, {1}", item["firstName"], item["lastName"]) , Percentage = (Convert. ToInt32(item["Marks"])/100) * 100 }).


4 Answers

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

No need of any lambda expresion as above.

like image 86
Jiss Avatar answered Oct 05 '22 03:10

Jiss


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.

like image 36
Øyvind Bråthen Avatar answered Oct 05 '22 02:10

Øyvind Bråthen


LINQ adds some sugar:

var stringArray = dr.ItemArray.Cast<string>().ToArray()
like image 42
cyrotello Avatar answered Oct 05 '22 02:10

cyrotello


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.

like image 27
Rune Grimstad Avatar answered Oct 05 '22 03:10

Rune Grimstad