tags:

views:

166

answers:

3

hello all,

i want to use jqgrid in my simple Asp.net application

now the problem when i am writing this code.

    public void  GetGridData(string sidx, string sord, int page, int rows)
    {
        return Content(JsonHelper.JsonForJqgrid(GetDataTable(sidx, sord, page, rows), rows, GetTotalCount(), page), "application/json");
    }

    public int GetTotalCount()
    {
        int totalCount = 0;
        try
        {
            using (GuestPassEntities demoMvcEntities1 = new GuestPassEntities())
            {

                totalCount = (from _product in demoMvcEntities1.GP_Register
                              select _product).Count();
            }
        }
        catch
        {
        }
        return totalCount;
    }
    public DataTable GetDataTable(string sidx, string sord, int page, int pageSize)
    {
        List<GP_Register> AllProducts = null;
        DataTable tab1 = new DataTable();
        using (GuestPassEntities demoMvcEntities1 = new GuestPassEntities())
        {

            AllProducts = (from _product in demoMvcEntities1.GP_Register.AsEnumerable()
                           select _product).ToList();



            tab1.Columns.Add("title", Type.GetType("System.String"));
            tab1.Columns.Add("desc", Type.GetType("System.String"));
            tab1.Columns.Add("select", Type.GetType("System.String"));

            foreach (GP_Register pnames in AllProducts)
            {
                DataRow dr = tab1.NewRow();
                dr["title"] = pnames.FullName.ToString();
                dr["desc"] = pnames.CompanyName.ToString();
                dr["select"] = pnames.EmployeeName.ToString();
                tab1.Rows.Add(dr);
            }
        }
        return tab1;
    }

how shall i return the contentresult.

Please suggest me any answers

Thanks Ritz

A: 

I wouldn't use a DataTable as the response set at all.. You can serialize a List where T is an object[]...

via Linq, you can do a

   ...Select(item => new object[] { 
     item.field1, 
     item.field2,
     ...
   });

If you use the default with a JsonResult (presuming you're doing ASP.Net MVC here), you can do a

   return JsonResult(new {
      total: totalRows
      ...
      rows: resultFromLinq /* .Select(new object[] {....}) */
   })

You can tie the underlying query into a sproc, or doing linq2sql or entities, nhibernate etc... however you want. Doing results from a DataTable to JSON directly doesn't serialize well. Selecting an object[]{} literal as your last line, will result in a serialized array of arrays.

{
  "total": 3,
  ...
  rows: [
    ["val1", "val2", 3, ...]
  ],
  ...
}

I would also take a look at Phil Haack's post on jqGrid

Tracker1
+1  A: 

If you have budget and can pay for components, the best idea is to use the commercial components Trirand (the same folks behind jqGrid client-side) ship

http://www.trirand.net/demo.aspx

A: 

Even if you do not use ASP.NET MVC, perhaps the following article would be helpful:

Using jqGrid’s search toolbar with multiple filters in ASP.NET MVC

Veleslav