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.
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
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.
This comment has been removed by the author.
ReplyDeleteSure ! Thanks for your comment . It's helping for other's !
ReplyDeleteVBConversions - Convert VB to C# with the best conversion tool on the market.Convert VB to C# with VBConversions,the most accurate code translation tool available.
ReplyDeleteany information then visit: www.vbconversions.com and Product Details : www.tangiblesoftwaresolutions.com/Product_Details/Instant_CSharp.html
vb.net to c# converter
vb.net to c#
ReplyDeletemore information then visit: www.vbconversions.com
Nice post very helpful
ReplyDeletedbakings