datatable/collection to Json

Source: Internet
Author: User

The jQueryUI framework for the front end gets the JSON-formatted data-bound display table.

The data obtained by the backend via WebService is a DataTable.

The DataTable to JSON is now available, and the data collection to JSON is also supported.

One. Project References: Newtonsoft.json

Two. function code, very simple:

Using System;
Using System.Data;

Namespace Webapplication1.common
{
public class Datatabletomodel
{

public static DataTable GetData ()
{
DataTable dt = new DataTable ();
Dt. Columns.Add ("UserId", typeof (Int32));
Dt. Columns.Add ("UserName", typeof (String));
Dt. Columns.Add ("Education", typeof (String));
Dt. Columns.Add ("Location", typeof (String));
Dt. Rows.Add (1, "Satinder Singh", "Bsc Com Sci", "Mumbai");
Dt. Rows.Add (2, "Amit Sarna", "Mstr Com Sci", "Mumbai");
Dt. Rows.Add (3, "Andrea Ely", "Bsc bio-chemistry", "Queensland");
Dt. Rows.Add (4, "Leslie Mac", "MSC", "Town-ville");
Dt. Rows.Add (5, "Vaibhav Adhyapak", "MBA", "New Delhi");
return DT;
}

<summary>
Data table binding information for the view layer
</summary>
<typeparam name= "T" ></typeparam>
public class viewbasemodel<t> where T:class
{
public int total {get; set;}
public int page {get; set;}
public int records {get; set;}
Public T rows {get; set;}
}

//<summary>
//Convert the specified column data in the DataTable to Json
//</summary>
//<typeparam name= "T" ></t Ypeparam>
//<param name= "DataList" ></PARAM>
//<param name= "ColumnNames" ></param
//<returns></returns>
public static string convert<t> (viewbasemodel<t> DataList) where T:class {
return Newtonsoft.Json.JsonConvert.SerializeObject (dataList);
}

<summary>
To convert the specified column data in a DataTable to Json
</summary>
<param name= "Total" > number of pages </param>
<param name= "Page" > Specify the page ordinal, starting at 1</param>
<param name= "records" > Total data </param>
<param name= "dtable" > Data collection </param>
<param name= "ColumnNames" > Display specified column in DataTable </param>
<returns> return JSON format data </returns>
public static string converttable (int total, int page, int records, DataTable dtable, params string[] columnnames) {

if (dtable = = NULL | | DTable.Rows.Count <= 0)
{
return string. Empty;
}

if (columnnames! = null && columnnames.length > 0)
{
Dtable = DTable.DefaultView.ToTable (false, ColumnNames);
}

Return convertcollection (Total, page, records, dtable);
}

<summary>
Turn data collection to Json
</summary>
<typeparam name= "T" > Specify Type Collection </typeparam>
<param name= "Total" > number of pages </param>
<param name= "Page" > Specify the page ordinal, starting at 1</param>
<param name= "records" > Total data </param>
<param name= "DataCollection" > Type Collection Objects </param>
<returns></returns>
public static string convertcollection<t> (int total, int page, int records, T datacollection) where T:class
{
if (datacollection = = null)
{
return string. Empty;
}

var Jsonmodel = new viewbasemodel<t> ();
jsonmodel.page = page;
Jsonmodel.records = records;
Jsonmodel.total = total;
Jsonmodel.rows = datacollection;

Return Newtonsoft.Json.JsonConvert.SerializeObject (Jsonmodel);
}
}
}

Three. External calls:

1.

DataTable dtable = Common.DataTableToModel.GetData ();
if (dtable = = NULL | | dtable. Rows.Count <= 0)
{
return View ();
}

DataTable newtable = dtable. Defaultview.totable (False, "UserName");
var Jsonmodel = new common.datatabletomodel.viewbasemodel<datatable> ();
Jsonmodel.page = 1;
jsonmodel.records = 1200;
jsonmodel.total = 100;
Jsonmodel.rows = newtable;

Viewbag.data = Common.DataTableToModel.Convert (Jsonmodel);

2.

Viewbag.data = Common.DataTableToModel.ConvertTable (1, 1, 1, Common.DataTableToModel.GetData ());

3.

public class DataRows {
public int sid {get; set;}
public string uname {get; set;}
Public DateTime addtime {get; set;}
}

var drlist = new List<datarows> {
New DataRows () {addtime=datetime.now, sid=1, uname= "test111"},
New DataRows () {addtime=datetime.now, sid=2, uname= "test222"},
New DataRows () {addtime=datetime.now, sid=3, uname= "test333"}
};

Viewbag.data = common.datatabletomodel.convertcollection<list<datarows>> (1, 1, 1,drList);

datatable/collection to Json

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.