如何将gridview数据转换为Json?

时间:2014-01-05 12:33:05

标签: c# asp.net json gridview

我有一个任务,我必须使用数据库中的数据创建一个gridview,并将信息从Gridview导出到Pdf和Json。

我完成了PDF部分,但现在我被困在Json部分,我不知道如何实现它。

var json = new JavaScriptSerializer().Serialize(GridViewStudents.Rows);
Response.ContentType = "application/json; charset=utf-8";
Response.AppendHeader("content-disposition", "attachment;filename=Students.txt");
Response.Write(json);
Response.End();

这就是我想要做的。我需要先从

转换数据
  1.   

    Gridview to Json

  2.   

    将数据导出到某个文件,如txt文件,该文件将显示可转换数据。

  3. 我已编写的代码给出了错误,其中包含

      

    在序列化“System.Web.UI.WebControls.GridViewRow”类型的对象时检测到循环引用

    任何人都可以帮助我完成这个并找到合适的解决方案。

    谢谢。

1 个答案:

答案 0 :(得分:1)

包含这些文件

<script src="~/Scripts/jquery.base64.js"></script>
<script src="~/Scripts/tableExport.js"></script>
<script src="~/Scripts/jspdf/libs/base64.js"></script>
<script src="~/Scripts/jspdf/jspdf.js"></script>
<script src="~/Scripts/jspdf/FileSaver.js"></script>
<script src="~/Scripts/jspdf/jspdf.plugin.cell.js"></script>

然后将这些功能添加到您的脚本

function ExportTpGridtoPDF(divid) {
    var table1 =
   tableToJson($('#' + divid + ' .grid-table').get(0)),
   cellWidth = 35,
   rowCount = 0,
   cellContents,
   //leftMargin = 2,
   //topMargin = 12,
   //topMarginTable = 55,
   //headerRowHeight = 13,
   //rowHeight = 9,
   leftMargin = 10,
   topMargin = 15,
   topMarginTable = 5,
   headerRowHeight = 13,
   rowHeight = 13,

    l = {
        orientation: 'l',
        unit: 'mm',
        format: 'a3',
        compress: true,
        fontSize: 8,
        lineHeight: 1,
        autoSize: false,
        printHeaders: true
    };

    var doc = new jsPDF(l, '', '', '');

    doc.setProperties({
        title: 'Test PDF Document',
        subject: 'This is the subject',
        author: 'author',
        keywords: 'generated, javascript, web 2.0, ajax',
        creator: 'author'
    });

    doc.cellInitialize();

    $.each(table1, function (i, row) {

        rowCount++;

        $.each(row, function (j, cellContent) {
            if (rowCount == 1) {
                doc.margins = 1;
                doc.setFontSize(12);

                doc.cell(leftMargin, topMargin, cellWidth, headerRowHeight, cellContent, i)
            }
            else if (rowCount == 2) {
                doc.margins = 1;
                doc.setFontSize(12);

                doc.cell(leftMargin, topMargin, cellWidth, rowHeight, cellContent, i);
            }
            else {

                doc.margins = 1;
                doc.setFontSize(12);

                doc.cell(leftMargin, topMargin, cellWidth, rowHeight, cellContent, i);
            }
        })
    })

    doc.save('sample Report.pdf');
}

function tableToJson(table) {
    var data = [];

    // first row needs to be headers
    var headers = [];
    for (var i = 0; i < table.rows[0].cells.length; i++) {
        if (table.rows[0].cells[i].innerHTML != "") {
            headers[i] = table.rows[0].cells[i].innerText.toLowerCase().replace(/ /gi, '');
        }
    }
    // go through cells
    for (var i = 1; i < table.rows.length; i++) {
        var tableRow = table.rows[i];
        var rowData = {};
        for (var j = 1; j < tableRow.cells.length; j++) {
            rowData[headers[j]] = tableRow.cells[j].innerText;
        }
        data.push(rowData);
    }
    return data;
}