Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Insert values from data set which contains one single row into a string array
Posted
Comments
johannesnestler 21-May-14 9:48am    
Sounds simple, so hard to tell wher you stuck... General answer: create an string array with a size like your column count (row has 5 columns, so array size needs to be 5 too). Then loop through all you (row-) columns and fill the Array for each. (don't Forget to convert all the values to string - which is just a call to ToString())
Francine DeGrood Taylor 21-May-14 13:11pm    
Create a List<object> (you don't have to know beforehand how many columns there are, and you can always ToArray it) and loop through your column values, getting the object value from the column. If you want to get fancy, convert it to string, then translate it into the appropriate data type using the column type (if you want to get that fancy). Depends on what you want to do with it after you put it into your collection.
Francine DeGrood Taylor 21-May-14 13:12pm    
Are you going to use the information as an array, or do you want to read it into a custom class object?

try like below
C#
string[] items = dataSet1.Tables[0].Rows[0].OfType<string>().ToArray();
 
Share this answer
 
v2
Some sample data to work with...
C#
// Set up a datatable containing a single row for the sake of example.
// If given a dataset then DataTable myTable = myDataSet.Tables[0];
DataTable myTable = new DataTable("Fred");
//  Some columns with random and arbitrary datatypes.
myTable.Columns.Add(new DataColumn("Int", typeof(Int32)));
myTable.Columns.Add(new DataColumn("Guid", typeof(Guid)));
myTable.Columns.Add(new DataColumn("DateTime", typeof(DateTime)));
myTable.Columns.Add(new DataColumn("Bool", typeof(Boolean)));
myTable.Columns.Add(new DataColumn("String", typeof(String)));

myTable.Rows.Add(new Object[] {1, Guid.NewGuid(),
                               DateTime.MaxValue, false, "a string"});

Extraction ...
C#
// Query the first (only) row in our demo table.
// Of course the ::ToString method for a datatype may not give you a format
// that you want but that's another problem.
IEnumerable<string> qryColumns = from object item in myTable.Rows[0].ItemArray
                                 select item.ToString();

// Dump the query results to a string array.
string[] op01 = qryColumns.ToArray<string>();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900