public static string DatatableToJson(DataTable objDT, int count = 0, string[] captionArray = null)
{
string json = string.Empty;
if (objDT != null && objDT.Rows.Count > 0)
{
string[] ColumnArr = objDT.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToArray();
var LinqResult = objDT.Rows.Cast<DataRow>().ToList();
List<List<string>> aaList1 = new List<List<string>>();
foreach (var item in LinqResult)
{
List<string> n = new List<string>();
for (int i = 0; i < ColumnArr.Length; i++)
{
n.Add(item[ColumnArr[i].ToString()].ToString());
}
aaList1.Add(n);
}
var formatedList = new
{
aaData = aaList1,
aaHeader = ColumnArr,
aaCount = count,
iTotalDisplayRecords = count,
aaCaption = captionArray
};
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
json = serializer.Serialize(formatedList);
}
else
{
json = "NO_RECORD";
}
return json;
}
{
string json = string.Empty;
if (objDT != null && objDT.Rows.Count > 0)
{
string[] ColumnArr = objDT.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToArray();
var LinqResult = objDT.Rows.Cast<DataRow>().ToList();
List<List<string>> aaList1 = new List<List<string>>();
foreach (var item in LinqResult)
{
List<string> n = new List<string>();
for (int i = 0; i < ColumnArr.Length; i++)
{
n.Add(item[ColumnArr[i].ToString()].ToString());
}
aaList1.Add(n);
}
var formatedList = new
{
aaData = aaList1,
aaHeader = ColumnArr,
aaCount = count,
iTotalDisplayRecords = count,
aaCaption = captionArray
};
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
json = serializer.Serialize(formatedList);
}
else
{
json = "NO_RECORD";
}
return json;
}
No comments:
Post a Comment