JQGrid does not display data

前端 未结 2 993
无人及你
无人及你 2020-12-20 09:32

I have created both client and server side for JQgrid and ASP.net. The grid is displayed but with no data. I could not see the result. The grid displayed but no data.

<
2条回答
  •  太阳男子
    2020-12-20 10:07

    I have modified the source, now records are displayed very smoothly but the only problem is that the search is not working, can you please have a look? My Codes are given below:

    The aspx page:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="JQGrid.aspx.cs" Inherits="JQGrid" %>
    
    
    
    
    
        
        <%----%>
    
        
        
        
        
        
    
        
    
    
    
        

    and the ashx:

    <%@ WebHandler Language="C#" Class="jqGridHandler" %>
    
    using System;
    using System.Collections.Generic;
    
    using System.Collections.ObjectModel;
    
    using System.Data;
    
    using System.Data.SqlClient;
    
    using System.Web;
    
    using System.Web.Script.Serialization;
    
    public class jqGridHandler : IHttpHandler
    {
    
        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
    
            string _search = request["_search"];
            string numberOfRows = request["rows"];
            //string numberOfRows = "10";
            string pageIndex= request["page"];
            string sortColumnName= request["sidx"];
            string sortOrderBy = request["sord"];
    
    
            int totalRecords;
            //public DataTable GetDataTable(string sidx, string sord, int page, int pageSize)
            Collection users = GetUsers(numberOfRows, pageIndex, sortColumnName, sortOrderBy, out totalRecords, _search);
            string output = BuildJQGridResults(users, Convert.ToInt32(numberOfRows), Convert.ToInt32(pageIndex), Convert.ToInt32(totalRecords));
            response.Write(output);
        }
    
        private string BuildJQGridResults(Collection users,int numberOfRows, int pageIndex,int totalRecords)
        {
    
            JQGridResults result = new JQGridResults();
            List rows = new List();
            foreach (User user in users)
            {
                JQGridRow row = new JQGridRow();
                row.id = user.CustomerID;
                row.cell = new string[8];
                row.cell[0] = user.CustomerID;
                row.cell[1] = user.CompanyName;
                row.cell[2] = user.ContactName;
                row.cell[3] = user.ContactTitle;
                row.cell[4] = user.Address;
                row.cell[5] = user.City;
                row.cell[6] = user.PostalCode;
                row.cell[7] = user.Country;
    
                rows.Add(row);
            }
            result.rows = rows.ToArray();
            result.page = pageIndex;
            result.total = totalRecords / numberOfRows;
            result.records = totalRecords;
            return new JavaScriptSerializer().Serialize(result);
        }
    
        private Collection GetUsers(string numberOfRows, string pageIndex, string sortColumnName, string sortOrderBy, out int totalRecords, string _search)
        {
            Collection users = new Collection();
            string connectionString = "Data Source=ritetechno\\sqlexpress;Initial Catalog=Northwind;Integrated Security=True";
            //
    
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand())
                {
                    int numRows=Convert.ToInt32(numberOfRows)*(Convert.ToInt32(pageIndex));
                    int excluderows=Convert.ToInt32(numberOfRows)*((Convert.ToInt32(pageIndex)-1));
                    command.Connection = connection;
                    command.CommandText = "SELECT TOP " + numRows + " CustomerID, CompanyName, ContactName, ContactTitle, Address, City, PostalCode, Country FROM Customers WHERE CustomerID NOT IN (SELECT TOP " + excluderows +" CustomerID FROM Customers)";
                    command.CommandType = CommandType.Text;
                    connection.Open();
    
                    using (SqlDataReader dataReader = command.ExecuteReader())
                    {
                        User user;
                        while (dataReader.Read())
                        {
                            user = new User();
                            user.CustomerID = Convert.ToString(dataReader["CustomerID"]);
                            user.CompanyName = Convert.ToString(dataReader["CompanyName"]);
                            user.ContactName = Convert.ToString(dataReader["ContactName"]);
                            user.ContactTitle = Convert.ToString(dataReader["ContactTitle"]);
                            user.Address = Convert.ToString(dataReader["Address"]);
                            user.City = Convert.ToString(dataReader["City"]);
                            user.PostalCode = Convert.ToString(dataReader["PostalCode"]);
                            user.Country = Convert.ToString(dataReader["Country"]);
                            users.Add(user);                        
                        } 
                    }
                    string cmdTotRec = "SELECT COUNT(*) FROM Customers";
                    SqlCommand chkTotRec = new SqlCommand(cmdTotRec, connection);
                    totalRecords = Convert.ToInt32(chkTotRec.ExecuteScalar().ToString());
                    connection.Close();
                }
    
                return users;
            }
    
        }
        public bool IsReusable
        {
            // To enable pooling, return true here.
            // This keeps the handler in memory.
            get { return false; }
        }
    
        public struct JQGridResults
        {
            public int page;
            public int total;
            public int records;
            public JQGridRow[] rows;
    
        }
        public struct JQGridRow
        {
            public string id;
            public string[] cell;
        }
    
        [Serializable]
        public class User
        {
            public string CustomerID { get; set; }
            public string CompanyName { get; set; }
            public string ContactName { get; set; }
            public string ContactTitle { get; set; }
            public string Address { get; set; }
            public string City { get; set; }
            public string PostalCode { get; set; }
            public string Country { get; set; }
        }
    }
    

提交回复
热议问题