私はこれに対する解決策を持っています。最初に を埋めADODB.Recordset
てから、 の列を作成しますDataGrid
。各列を Recordset の列にバインドする必要があります。次に、OleDb.OleDbDataAdapter
に接続するために使用しRecordset
ていDataTable
ます。次に、を設定し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