如何在gridview中显示数据

时间:2016-04-04 09:41:30

标签: sql vb.net gridview devexpress xtragrid

我想将数据从数据库显示到gridview。但是,当我进行调试时,数据由datareader读取,但无法将其显示在网格中。 你能帮忙解决这个问题吗? 这是我的代码。

        Dim strSQL_ = "SELECT FDREF, FDESTKCODE, FDESTKEXP, FDEAMOUNT, FDEUNIT, FDEPRICE, " & _
            "FDETOTALPRICE, FMAREF FROM FICHEDETAIL WHERE FMAREF = '" & fmaref3_ & "'"
    Dim dsx1 = New DataSet
    Dim adx1 As New SqlClient.SqlDataAdapter(strSQL, objCon)
    adx1.Fill(dsx1, "a")
    Dim t1 As DataTable = dsx1.Tables("a")
    mygrid.DataSource = dsx1.Tables("a")

    objCon.Close()

    Dim sqlCmd_ As New SqlCommand(strSQL_, objCon)
    objCon.Open()
    sqlCmd_.ExecuteNonQuery()

    Dim dreader_ As SqlDataReader
    dreader_ = sqlCmd_.ExecuteReader

    If dreader_.Read Then

        Dim a As Integer
        For a = 0 To GridView6.RowCount - 1
            Dim ref_ As String = dreader_.GetInt32(dreader_.GetOrdinal("FDREF")).ToString()
            ref_ = dreader_.GetString(dreader_.GetOrdinal("FDESTKCODE")).ToString()
            GridView6.SetRowCellValue(a, "FDESTKCODE", ref_)
            ref_ = dreader_.GetString(dreader_.GetOrdinal("FDESTKEXP")).ToString()
            GridView6.SetRowCellValue(a, "FDESTKEXP", ref_)
            ref_ = dreader_.GetDouble(dreader_.GetOrdinal("FDEAMOUNT")).ToString()
            GridView6.SetRowCellValue(a, "FDEAMOUNT", ref_)
            ref_ = dreader_.GetString(dreader_.GetOrdinal("FDEUNIT")).ToString()
            GridView6.SetRowCellValue(a, "FDEUNIT", ref_)
            ref_ = dreader_.GetDecimal(dreader_.GetOrdinal("FDEPRICE")).ToString()
            GridView6.SetRowCellValue(a, "FDEUNIT", ref_)
            ref_ = dreader_.GetDecimal(dreader_.GetOrdinal("FDETOTALPRICE")).ToString()
            GridView6.SetRowCellValue(a, "FDETOTALPRICE", ref_)

        Next
    End If

1 个答案:

答案 0 :(得分:0)

此时For的代码永远不会被执行GridView6.RowCount = 0

我认为你想要做的是

Dim strSQL = "SELECT FDREF, FDESTKCODE, FDESTKEXP, FDEAMOUNT, FDEUNIT, FDEPRICE, " & _
            "FDETOTALPRICE, FMAREF FROM FICHEDETAIL WHERE FMAREF = '" & fmaref3_ & "'"
    Dim dsx1 = New DataSet
    Dim adx1 As New SqlClient.SqlDataAdapter(strSQL, objCon)
    adx1.Fill(dsx1, "a")
    Dim t1 As DataTable = dsx1.Tables("a")
    mygrid.DataSource = t1

    objCon.Close()