easyui datagrid动态绑定列名和数据

ashx文件内容 
public void ProcessRequest(HttpContext context)
    {//示例用,各位可以根据自己需求写  

        var id = context.Request["id"];
        if (id.ToString().Equals("1"))
        {
            DataTable dt = createTable();

            DataRow dr = dt.NewRow();
            dr["field"] = "building_id";
            dr["title"] = "公寓编号";
            dr["align"] = "center";
            dr["width"] = 100;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["field"] = "building_name";
            dr["title"] = "公寓名称";
            dr["align"] = "center";
            dr["width"] = 100;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["field"] = "building_info";
            dr["title"] = "公寓信息";
            dr["align"] = "center";
            dr["width"] = 100;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["field"] = "school_area";
            dr["title"] = "所在校区";
            dr["align"] = "center";
            dr["width"] = 100;
            dt.Rows.Add(dr);

            //string sql = "select building_id,building_name,building_info,school_area from building";
            //string countsql = "select count(*) from building";
            //int count = DBHelper.GetScalar(countsql);
            //DataTable dtt = DBHelper.GetDataSet(sql);

            DataTable dtt = new DataTable();
            dtt.Columns.Add("building_id");
            dtt.Columns.Add("building_name");
            dtt.Columns.Add("building_info");
            dtt.Columns.Add("school_area");
            DataRow drTemp;
            for (int i = 0; i < 10; i++)
            {
                drTemp = dtt.NewRow();
                drTemp["building_id"] = "A" + i.ToString();
                drTemp["building_name"] = "A" + i.ToString() + "号楼";
                drTemp["building_info"] = "A" + i.ToString() + "公寓";
                drTemp["school_area"] = "A" + i.ToString() + "小营";
                dtt.Rows.Add(drTemp);
            }
            int count = dtt.Rows.Count;
            Hashtable ht = new Hashtable();
            ht.Add("total", count);
            ht.Add("columns", dt);
            ht.Add("rows", dtt);
            string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(ht);


            context.Response.Clear();
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.ContentType = "application/json";
            context.Response.Write(strJson);
            context.Response.Flush();
            context.Response.End();
        }
        else
        {
            DataTable dt = createTable();

            DataRow dr = dt.NewRow();
            dr["field"] = "building_id";
            dr["title"] = "公寓编号";
            dr["align"] = "center";
            dr["width"] = 100;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["field"] = "building_name";
            dr["title"] = "公寓名称";
            dr["align"] = "center";
            dr["width"] = 100;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["field"] = "school_area";
            dr["title"] = "所在校区";
            dr["align"] = "center";
            dr["width"] = 100;
            dt.Rows.Add(dr);

            //string sql = "select building_id,building_name,school_area from building";
            //string countsql = "select count(*) from building";
            //int count = DBHelper.GetScalar(countsql);
            //DataTable dtt = DBHelper.GetDataSet(sql);
            DataTable dtt = new DataTable();
            dtt.Columns.Add("building_id");
            dtt.Columns.Add("building_name");
            dtt.Columns.Add("building_info");
            dtt.Columns.Add("school_area");
            DataRow drTemp;
            for (int i = 0; i < 10; i++)
            {
                drTemp = dtt.NewRow();
                drTemp["building_id"] = "B" + i.ToString();
                drTemp["building_name"] ="B"+ i.ToString() + "号楼";
                drTemp["building_info"] = "B"+i.ToString() + "公寓";
                drTemp["school_area"] = "B"+i.ToString() + "小营";
                dtt.Rows.Add(drTemp);

            }
            int count = dtt.Rows.Count;


            Hashtable ht = new Hashtable();
            ht.Add("total", count);
            ht.Add("columns", dt);
            ht.Add("rows", dtt);
            string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(ht);


            context.Response.Clear();
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.ContentType = "application/json";
            context.Response.Write(strJson);
            context.Response.Flush();
            context.Response.End();
        }

    }

    public DataTable createTable()
    {

        DataTable dt = new DataTable("myTable");

        //field列  
        DataColumn columnField = new DataColumn();//创建一列  
        columnField.DataType = System.Type.GetType("System.String");//数据类型  
        columnField.ColumnName = "field";//列名  
        dt.Columns.Add(columnField);//添加到table  
        //title列  
        DataColumn columnTitle = new DataColumn();
        columnTitle.DataType = System.Type.GetType("System.String");
        columnTitle.ColumnName = "title";
        dt.Columns.Add(columnTitle);
        //align列  
        DataColumn columnAlign = new DataColumn();
        columnAlign.DataType = System.Type.GetType("System.String");
        columnAlign.ColumnName = "align";
        dt.Columns.Add(columnAlign);
        //width列  
        DataColumn columnWidth = new DataColumn();
        columnWidth.DataType = System.Type.GetType("System.Int32");
        columnWidth.ColumnName = "width";
        dt.Columns.Add(columnWidth);

        return dt;
    }  
 
    public bool IsReusable {
        get {
            return false;
        }
    }

前台
<pre name="code" class="html"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>DataGrid动态生成列</title>
    <link id="Link1" rel="stylesheet" href="../jQEasyUI/jquery-easyui-1.4.2/themes/default/easyui.css"
        type="text/css" />
    <link rel="stylesheet" href="../jQEasyUI/jquery-easyui-1.4.2/themes/icon.css" type="text/css" />
    <script type="text/javascript" src="../jQEasyUI/jquery-easyui-1.4.2/jquery.min.js"
        charset="utf-8"></script>
    <script type="text/javascript" src="../jQEasyUI/jquery-easyui-1.4.2/jquery.easyui.min.js"
        charset="utf-8"></script>

    <script type="text/javascript">
        var grid;

        $(function () {

            grid = $('#tt').datagrid({
                fit: true,//自动大小    
                rownumbers: true,//行号   
                loadMsg: '数据装载中......',
                singleSelect: true,//单行选取  
                pagination: false,//显示分页  
                //height:500px,
                //width:1000px,
                toolbar: '#tabs',
                columns: [[]],
                toolbar: [{
                    text: '显示1',
                    iconCls: 'icon-add',
                    handler: newData
                }, '-', {
                    text: '显示2',
                    iconCls: 'icon-add',
                    handler: newData2
                }]
            });

            //  self.parent.$("#tabs").tabs("loaded");
        });

        function newData() {

            $.post('Test.ashx', { id: 1 },
                function (data) {
                    grid.datagrid({
                        columns: [data.columns]
                    }).datagrid("loadData", data);
                }, 'json');
        }

        function newData2() {

            $.post('Test.ashx', { id: 2 },
                function (data) {
                    grid.datagrid({
                        columns: [data.columns]
                    }).datagrid("loadData", data);
                }, 'json');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div style="height: 1000px">
            <table id="tt" style="height: 535px">
            </table>
        </div>
    </form>
</body>
</html>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值