I have this kind of datatable:
Name | CategorieID | FullCategorie_ID ---- ------------- ---------------- A 1 12 B 1 13 C 5 14 D 3 15 E 6 16
I want to save the values of column Name
in an array, I am using this to get the values of a row. E.g. To get the values of the first row I can use the following code.
var stringArr =datatable1.Rows[0].ItemArray.Select(x => x.ToString()).ToArray();
But I don't know how to get all the values of the only first column.
Replace type with the data type of your single column (e.g. int , string , ...) and myField with the name of your single column. var myArray = (from row in myDataTable. AsEnumerable() select row. Field<type>("myField")).
Very easy: var stringArr = dataTable. Rows[0]. ItemArray.
Select(row => row["columnName"]. ToString()). ToArray();
I suggest to use LINQ to DataSet for querying DataTable:
datatable1.AsEnumerable().Select(r => r.Field<string>("Name")).ToArray();
Mode classic
List<String> stringArr = new List<String>(); // Classic version :-) for( int a = 0 ; a < datatable1.Rows.Count ; a ++) { stringArr.Add(datatable1.Rows[a]["Name"].ToString()); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With