0

如何将访问数据库中的数据添加到 wpf 数据网格。我曾尝试使用数据表作为数据源,但它给出了一个错误。我也尝试过使用 itemsource 而不是 datasource 就像一些消息来源建议的那样,但徒劳无功。实际上我在用代码填充我的数据表后被卡住了:

    `Dim da As New OleDb.OleDbDataAdapter("SELECT activitynm as [Activity],motonum as [Car No], " & _
                                         "startmile as [Start mileage],stopmile as [Stop mileage],proj_code  as [Project]," & _
                                         "ac_code as [Account code],month(miledate) as [Project month], year(miledate) as [Project year]" & _
                                         " from carmileage where ac_code='""'", cnn2)
    Dim dt As New DataTable
    'fill data to datatable
    da.Fill(dt)
    //What to put here is the problem`

有人有线索。请帮忙

4

2 回答 2

0

设置 itemsource 属性:

ID_Of_datagrid.ItemSource = dt.rows

一个显示简单绑定的教程另一个显示简单绑定的教程

于 2013-01-14T17:38:35.190 回答
0

我有解决方案。我首先填充ADODB.Recordset,然后为DataGrid. 您必须将每一列绑定到 Recordset 中的列,然后我OleDb.OleDbDataAdapter用来连接RecordsetDataTable. 然后你设置DataGrid.ItemsSource = DataTable.DefaultView.

这是我的代码(我也对列进行了一些字符串格式化):

 Public Sub Polni_Tabelo()
    Dim strQuery As String ' SQL stavek 
    Dim rsADO As New ADODB.Recordset  'Recordset iz baze
    Dim col(9) As DataGridTextColumn ' Stolpci datagrid text colmns
    Dim colCheck As New DataGridCheckBoxColumn
    Dim myDataAdapter = New OleDb.OleDbDataAdapter ' adapter za prenašanje v datatable (public)
    ' Dim dr As DataRowView

    Dim textStyle = New Style(GetType(TextBlock))
    textStyle.Setters.Add(New Setter(TextBlock.TextWrappingProperty, TextWrapping.Wrap))

    rsADO.CursorLocation = ADODB.CursorLocationEnum.adUseServer  'definicija lokacije kurzorja ? poglej W3Scool
    rsADO.CursorType = ADODB.CursorTypeEnum.adOpenDynamic
    rsADO.LockType = ADODB.LockTypeEnum.adLockOptimistic
    Try
        strQuery = "SELECT" & _
                    " Po.ID_Ponudbe AS ID" & _
                    ", Po.Datum_Ponudbe AS Datum" & _
                    ", Po.St_Ponudbe AS Stevilka" & _
                    ", Podjetja.Naziv_Podjetja AS Podjetje" & _
                    ", Po.Opis" & _
                    ", Po.Cena_Ponudbe AS Cena" & _
                    ", Po.DDV" & _
                    ", Po.SALDO" & _
                    ", Materialni_Stroski AS Strošek" & _
                    ", Po.Narocilnica AS Naročilnica" & _
                    ", Po.Naroceno AS Naročeno" & _
                    " FROM Podjetja" & _
                    " INNER JOIN" & _
                    " Ponudbe AS Po ON Podjetja.ID_Podjetja = Po.ID_Podjetja"  'Stavek za branje celotne tabele materiala
        'strQuery = "SELECT * From Ponudbe Where ID_Ponudbe = 2"
        rsADO.Open(strQuery, Application.conAdo)   'polni queri preko povezave ustvarjene v mainOknu
        PonudbeDataGrid.Columns.Clear()
        'i = rsADO.RecordCount
        For i = 1 To 9

            col(i) = New DataGridTextColumn                 'definiraj nov stolpec
            col(i).Header = rsADO(i).Name                   'definiraj ime stolpca
            col(i).Binding = New Binding(rsADO(i).Name)                     'definiraj povezavo na bazo in format stolpca
            col(i).MaxWidth = 300
            Select Case i
                Case 1
                    col(i).Binding.StringFormat = "{0:dd.MM.yyyy}"
                Case 4
                    col(i).ElementStyle = textStyle
                    col(i).MaxWidth = 400
                    col(i).MinWidth = 100
                Case 5, 6, 7, 8
                    col(i).Binding.StringFormat = "{0:#,##0.00 €}" ' "x.xx0,00 €"
                Case Else

            End Select
            PonudbeDataGrid.Columns.Add(col(i))            'dodaj stolpec v datagrid
        Next


        colCheck.Header = rsADO(10).Name
        colCheck.Binding = New Binding(rsADO(10).Name)
        colCheck.CanUserResize = False
        PonudbeDataGrid.Columns.Add(colCheck)
        'MaterialDataGrid.Style.Setters.Add(New Setter(Control.VerticalAlignmentProperty, VerticalAlignment.Center))

        PonudbeDataGrid.FontSize = Convert.ToInt32(Me.Font.Text)
        PonudbeDataGrid.ColumnHeaderHeight = 30
        PonudbeDataGrid.MinRowHeight = 30


        myDataTable = New DataTable                         'ustvari novo DataTabelo 
        myDataAdapter.Fill(myDataTable, rsADO)              'polni DataTabelo z recordsetom

        With PonudbeDataGrid
            .ItemsSource = myDataTable.DefaultView          'Vnos DataTable v datagrid
        End With
        PonudbeDataGrid.UpdateLayout()

        rsADO.Close()



    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    myDataAdapter = Nothing
    rsADO = Nothing

    myDataTable.DefaultView.Sort = "Stevilka"
End Sub
于 2015-05-06T15:23:43.977 回答