比如上图,怎么样从sql里面查询所需要的数据,然后让他绑定到datagrid中的。求高手在空余时空能指导一二。要是能有具体的源代码就好了。在百度上找到的都没有包括数据库的。jquerydatagrid.netsql

解决方案 »

  1.   

    楼上的在前端部分已经很详细了,
    补充一下.cs文件里的,注意引用Newtonsoft.Json,这个组件用来序列化集合或者DataTable为json,dt 可以为DataTable,也可以为List<T>,
                        object obj = new { total = total, rows = dt };
                        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);                    Response.Write(response);
      

  2.   

    很感谢各位高手看到我的菜鸟帖子并给予指导。其实这些代码我在其他地方也看了,试了几次都没弄出来(我太菜了),你们有没有完整的代码呢?发一份给我吧。我的扣扣邮箱:[email protected],不胜感激!
      

  3.   

    根据各位高手的指导,弄出了下面的:
    下面是  .aspx代码
    //datagrid.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="datagrid.aspx.cs" Inherits="datagrid" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>datagrid应用</title>
        <script src="easyui1.3/jquery-1.8.0.min.js" type="text/javascript"></script>
        <script src="easyui1.3/jquery.easyui.min.js" type="text/javascript"></script>
        <link href="easyui1.3/themes/default/easyui.css" rel="stylesheet" type="text/css" />
        <link href="easyui1.3/themes/icon.css" rel="stylesheet" type="text/css" />
        <script src="easyui1.3/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
        <script type="text/javascript" language="javascript">
            $(function () {
                $('#MailTable').datagrid({
                    url: 'datagrid.ashx',
                    striped: true,
                    title: "邮件发送"
                })
            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <table id="MailTable" class="easyui-datagrid" 
                data-options="singleSelect:true,collapsible:true,rownumbers:true,autoRowHeight:false,height:300,width:360,pagination:true,pageSize:5">
                <thead>
                    <tr>
                        <th data-options="field:'Id',sortable:true">
                            ID
                        </th>
                        <th data-options="field:'MailFrom',sortable:true">
                            发信人
                        </th>
                        <th data-options="field:'Date',sortable:true">
                            时间
                        </th>
                        <th data-options="field:'MailTo',sortable:true">
                            收信人
                        </th>
                    </tr>
                </thead>
            </table>
        </div>
        </form>
    </body>
    </html>下面是 .ashx文件代码
    //datagrid.ashx
    <%@ WebHandler Language="C#" Class="datagrid" %>using System;
    using System.Web;
    using System.Web.Configuration;
    using System.Data;
    using System.Data.SqlClient;public class datagrid : IHttpHandler
    {    public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            string connString = WebConfigurationManager.ConnectionStrings["SqlConnection"].ToString();
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            string sqlstr = "select * from mail";
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
            da.Fill(ds);
            context.Response.Write(this.DataTableToJson(ds.Tables[0], ds.Tables[0].Rows.Count.ToString()));
            conn.Close();
        }    public string DataTableToJson(DataTable dt, string TotalRecord)
        {
            string jsonstr = "{\"total\":" + TotalRecord + ",\"rows\":[";
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                jsonstr = jsonstr + "{";
                for (int i = 0; i < dt.Columns.Count; i++)
                {                jsonstr = jsonstr + "\"" + dt.Columns[i].ColumnName + "\":\"" + dt.Rows[j][i] + "\"";
                    if (i != dt.Columns.Count - 1)
                    {
                        jsonstr = jsonstr + ",";
                    }
                }
                if (j == dt.Rows.Count - 1)
                {
                    jsonstr = jsonstr + "}";
                }
                else
                {
                    jsonstr = jsonstr + "},";
                }
            }
            jsonstr = jsonstr + "]}";
            return jsonstr;
        }    public bool IsReusable
        {
            get
            {
                return false;
            }
        }}
    下面是 web.config代码
    <?xml version="1.0"?><!--
      有关如何配置 ASP.NET 应用程序的详细信息,请访问
      http://go.microsoft.com/fwlink/?LinkId=169433
      --><configuration>
      <connectionStrings>
        <add name="SqlConnection" providerName="System.Data.SqlClent" connectionString="server=.;uid=sa;pwd=123;database=test" />
      </connectionStrings>
      <system.web>
        <compilation debug="false" targetFramework="4.0" />
      </system.web></configuration>