jqGrid不使用JSON显示SQL Server数据

时间:2013-03-13 00:22:37

标签: sql vb.net json jqgrid

我已经搞砸了几天了,我正在失去理智。我能够使用'本地'数据类型让jqGrid正常工作,现在我正试图将它与我的SQL数据联系起来。我没有错误,只是一个空格。我在网上搜索并尝试了很多没有运气的例子。网格显示和列​​标题出现,只是没有数据。我也没有收到任何js错误。在这一点上,我对分页,排序,搜索等不感兴趣。只需要显示数据。有人能指出我正确的方向吗?

VB.NET code:
============
Public Function GetGridData() As JqGridData
    Dim sql As String = "Select unit_number, product_code, blood_type, 'No Match' scan_result From product_header Where (product_id < 20) Order By 1, 2, 3"

    Dim dt As New DataTable()
    Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("conn").ConnectionString)
    Dim adapter As New SqlDataAdapter(sql, conn)
    adapter.Fill(dt)

    Dim result As New JqGridData()
    Dim gridRows As New List(Of TableRow)()
    Dim idx As Integer = 1

    For Each row As DataRow In dt.Rows
        Dim newrow As New TableRow()
        newrow.id = idx
        newrow.cell = New List(Of String)(4)
        newrow.cell(0) = row(0).ToString()
        newrow.cell(1) = row(1).ToString()
        newrow.cell(2) = row(2).ToString()
        newrow.cell(3) = row(3).ToString()
        gridRows.Add(newrow)
    Next

    result.total = 2
    result.page = 1
    result.records = gridRows.Count
    result.rows = gridRows

    Return result
End Function

Public Class TableRow

    Private m_id As Integer
    Private m_cell As List(Of String)

    Public Property id() As Integer
        Get
            Return m_id
        End Get
        Set(ByVal value As Integer)
            m_id = value
        End Set
    End Property

    Public Property cell() As List(Of String)
        Get
            Return m_cell
        End Get
        Set(ByVal value As List(Of String))
            m_cell = value
        End Set
    End Property

End Class

Public Class JqGridData

    Private m_total As Integer
    Private m_page As Integer
    Private m_records As Integer
    Private m_rows As List(Of TableRow)

    Public Property total() As Integer
        Get
            Return m_total
        End Get
        Set(ByVal value As Integer)
            m_total = value
        End Set
    End Property

    Public Property page() As Integer
        Get
            Return m_page
        End Get
        Set(ByVal value As Integer)
            m_page = value
        End Set
    End Property

    Public Property records() As Integer
        Get
            Return m_records
        End Get
        Set(ByVal value As Integer)
            m_records = value
        End Set
    End Property

    Public Property rows() As List(Of TableRow)
        Get
            Return m_rows
        End Get
        Set(ByVal value As List(Of TableRow))
            m_rows = value
        End Set
    End Property

End Class

js code:
========
$(document).ready(function(){
    $("#list").jqGrid({
        url: 'Display.aspx/GetGridData',
        datatype: 'json',
        mtype: "GET",
        ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
        serializeGridData: function (data) {
            return JSON.stringify(data);
        },
        jsonReader: {
            root: "rows",
            page: "page",
            total: "total",
            records: "records",
            repeatitems: true
        },
        colModel: [
            {name: 'unit_number', label: 'Unit Number', width: 100, align: 'center'},
            {name: 'product_code', label: 'Product Code', width: 200, align: 'center'},
            {name: 'blood_type', label: 'Blood Type', width: 200, align: 'center'},
            {name: 'scan_result', label: 'Scan Result', width: 200, align: 'center'}
        ],
        rowNum: 10,
        rowList: [10, 20, 30],
        sortname: 'id',
        sortorder: "asc",
        pager: "#pager",
        viewrecords: true,
        caption:  'My first grid'
    }).jqGrid('navGrid', '#pager', {edit: false, add: false, del: false, search: true});
});

1 个答案:

答案 0 :(得分:0)

尝试拔出

    ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
    serializeGridData: function (data) {
        return JSON.stringify(data);
    },
    jsonReader: {
        root: "rows",
        page: "page",
        total: "total",
        records: "records",
        repeatitems: true
    },

并确保传递jqGrid所需的所有数据。我不是VB.Net的人,所以我也假设你有一个控制器/动作为你的网格提供数据?您也可以尝试手动为网格构建行以测试该方面。