比如上图,怎么样从sql里面查询所需要的数据,然后让他绑定到datagrid中的。求高手在空余时空能指导一二。要是能有具体的源代码就好了。在百度上找到的都没有包括数据库的。jquerydatagrid.netsql
解决方案 »
- 关于 this.ClientScript.RegisterOnSubmitStatement()的问题
- 请教大侠,在GridView的空模版有个FormView控件,如何获得这个FormView控件呢?
- .net session保存在数据库中,生成的APPID不一样,求帮助。
- 我用到的三种SQLHelper(二)详细用法
- 关于SESSION丢失的问题
- this.TextBox1.Text = row.Cells[1].Text;
- DataGind 分页显示问题
- conn.asp我是SQL数据库怎么修改文件
- 多行Textbox C#控制中如何在任意位置添加字符(高分, 在线等)?
- button控件怎么上传两个参数
- SqlBulkCopy 从数据库中一个表添加到另一个表。列数不同的时候
- 求分词查询SQL语句或思路
补充一下.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);
下面是 .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>