all table value.. in that arry..... ?? All table from dataset right ? then you can use below code
ArrayList myArrayList = new ArrayList();
for (int i = 0; i < ds.Tables.Count; i++)
{
foreach (DataRow dtRow in ds.Tables[i].Rows)
{
myArrayList.Add(dtRow);
}
}
Many time we need need to get data from dataset\Datatable to array to pass it to some function or third API like x axis or y axis series of chart controls or to some other reporting or data binding control may be dropdown. it becomes difficult to get
the data of a column(s) in array and finally we end up writing a for loop. here is a sample example by which we can simply get array of a column(s) with out a for loop
public static string DataRowToString(DataRow dr)
{
return dr["columns"].ToString();
}
public string [] DataTableToArray(DataTable dt)
{
DataRow[] dr = dt.Select();
string [] strArr= Array.ConvertAll(dr, newConverter(DataRowToString));
return strArr;
}
Array.ConvertAll() function simply converts the array of one to another so here in above example we are passing dr as an araay of datarows in first arguments. second
argument is aConverter delegate type which convert one type to another so in this we will pass a link to delegate which is DataRowToString() function ,the delegate
should know the input datatype and output datatype which is datatrow and string in this case respectively. function DataRowToString noe simply takes a datarow and return the string value for the column specified. which can be extended for multiple columns
also.
SudhaRubini
Member
391 Points
383 Posts
How to convert a dataset to string array
Jan 21, 2013 07:44 AM|LINK
Hi All,
I am fetching data from the database into a dataset now what i want to do is convert the data of the dataset table into a array of String...
I have for 5 field in that dataset table... My view is reuse the value.....
can you show me how to do that.............
sanjayverma_...
Participant
1410 Points
330 Posts
Re: How to convert a dataset to string array
Jan 21, 2013 07:46 AM|LINK
Hi SudhaRubini,
Is it you want perticular column in array ?
amolpandit22
Participant
1468 Points
332 Posts
Re: How to convert a dataset to string array
Jan 21, 2013 07:50 AM|LINK
Using ArrayList you can achive this, refer below sample code
foreach (DataRow dtRow in myDataSet.Tables[0].Rows) { myArrayList.Add(dtRow); } return myArrayList;SudhaRubini
Member
391 Points
383 Posts
Re: How to convert a dataset to string array
Jan 21, 2013 07:52 AM|LINK
No. all table value.. in that arry.....
amolpandit22
Participant
1468 Points
332 Posts
Re: How to convert a dataset to string array
Jan 21, 2013 09:09 AM|LINK
all table value.. in that arry..... ?? All table from dataset right ? then you can use below code
ArrayList myArrayList = new ArrayList(); for (int i = 0; i < ds.Tables.Count; i++) { foreach (DataRow dtRow in ds.Tables[i].Rows) { myArrayList.Add(dtRow); } }jeevajsb
Member
159 Points
376 Posts
Re: How to convert a dataset to string array
Jan 21, 2013 10:17 AM|LINK
// Declaring Array string[,] ArrayValues=new string[ DataTable.Rows.Count,datatable1.Columns.Count]; for(int row = 0; row < DataTable.Rows.Count; ++row) { for(int col = 0; col < DataTable.Columns.Count; col++) { // inserting Array values from DataTable ArrayValues[row, col] = DataTable.Rows[row][col].ToString(); } }Jeeva Jsb.
[Mark as Answer on the post that helps you]
dotnetlover ...
Member
66 Points
30 Posts
Re: How to convert a dataset to string array
Jan 21, 2013 10:25 AM|LINK
Many time we need need to get data from dataset\Datatable to array to pass it to some function or third API like x axis or y axis series of chart controls or to some other reporting or data binding control may be dropdown. it becomes difficult to get the data of a column(s) in array and finally we end up writing a for loop. here is a sample example by which we can simply get array of a column(s) with out a for loop
public static string DataRowToString(DataRow dr) { return dr["columns"].ToString(); } public string [] DataTableToArray(DataTable dt) { DataRow[] dr = dt.Select(); string [] strArr= Array.ConvertAll(dr, newConverter(DataRowToString)); return strArr; }Array.ConvertAll() function simply converts the array of one to another so here in above example we are passing dr as an araay of datarows in first arguments. second argument is aConverter delegate type which convert one type to another so in this we will pass a link to delegate which is DataRowToString() function ,the delegate should know the input datatype and output datatype which is datatrow and string in this case respectively. function DataRowToString noe simply takes a datarow and return the string value for the column specified. which can be extended for multiple columns also.
please check
http://forums.asp.net/t/1328837.aspx/1