将datatable与datagrid asp.net连接起来

时间:2017-03-13 12:12:03

标签: vb.net winforms

所以我试图将我的datagrid绑定到数据表,我想出了这段代码 现在一切似乎都在工作,但它并没有显示我的gridview它甚至没有显示数据网格,当我执行任何人知道什么是问题,因为我无法弄清楚一切应该工作正常但gridview不加载。

  Dim mrDB As New Odbc.OdbcConnection(myDAC.OBDC)
    Dim dgrMR As New DataTable
    Dim dr As DataRow

    dgrMR.Columns.Add("Data")
    dgrMR.Columns.Add("Utente")
    dgrMR.Columns.Add("Nome")
    dgrMR.Columns.Add("Série")
    dgrMR.Columns.Add("Tipo")
    dgrMR.Columns.Add("Estado")

    dgvMarcacoes.Columns.Clear()


    Dim sqlStr As String = "select distinct data,idno,idnome, u_tratam as serie, u_tipomr, Max(estado) as estado " &
                            "from [marca].[mr]"

    sqlStr += queryFilter
    sqlStr += "group by idno,idnome, u_tratam, data, u_tipomr "
'this msgbox shows a correct string (that works on sql) so the problem is not on this part
    'MsgBox(sqlStr, MsgBoxStyle.OkOnly, "controlo")

    mrDB.Open()
    Dim mrComm As New Odbc.OdbcCommand(sqlStr, mrDB)

    Dim mrReader = mrComm.ExecuteReader
    Dim b = New StringBuilder()
    If Not (mrReader.HasRows) Then
        Me.dgvMarcacoes.Visible = False
        MsgBox("Não há marcações para o dia indicado", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "ATENÇÃO...")
    Else
        'Me.dgvMarcacoes.Visible = True
        While mrReader.Read
            dr = dgrMR.NewRow()

            dr(0) = Trim(mrReader(0))
            dr(1) = Trim(mrReader(1))
            dr(2) = Trim(mrReader(2))
            dr(3) = Trim(mrReader(3))
            dr(4) = Trim(mrReader(4))
            dr(5) = Trim(mrReader(5))
            dgrMR.Rows.Add(dr)

            'dr.Item(5).Value = Trim(estado(mrReader(5)))

        End While

    End If

    dgvMarcacoes.DataSource = dgrMR
    dgvMarcacoes.DataBind()
    mrDB.Close()
'so i tried this to see if the data was passing correctly to my datatable and it is :\
    For i = 0 To dgrMR.Rows.Count - 1
        For u = 0 To dgrMR.Columns.Count - 1
            MsgBox(dgrMR.Rows(i)(u).ToString)
        Next
    Next

1 个答案:

答案 0 :(得分:0)

您有一个标记asp.net,但您的代码中有MsgBox表示这是一个WinForm应用程序。检查网格列是否为AutogenerateColumns=True并将其更改为false