我可以使用以下代码将数据加载到数据网格控件中: 子LoadDB()
Const adoStateOpen As Integer = 1
Dim oConn As Object
Dim rsResults As Object
Dim oDataGrid As Object
Dim szSQLServer As String
Dim szSQLDatabase As String
Dim szSQLUserID As String
Dim szSQLPassword As String
Dim szSQLQuery As String
Dim szSQLConnectionString As String
On Error GoTo ErrorHandler
szSQLServer = "ServerName"
szSQLDatabase = "DB1"
szSQLUserID = "sa"
szSQLPassword = ""
szSQLQuery = "Select cutomerID from customers"
Set oConn = CreateObject("ADODB.Connection")
Set rsResults = CreateObject("ADODB.Recordset")
'set the oDataGrid object
Set oDataGrid = CimGetObject().GetObject("dgDataGrid").OleObject
szSQLConnectionString = "Provider=SQLNCLI11;Data Source=" & szSQLServer & _
";Database=" & szSQLDatabase & _
";UID=" & szSQLUserID & _
";PWD=" & szSQLPassword & ";"
oConn.ConnectionString = szSQLConnectionString
'Open Database connection
oConn.Open
'Run the SQL Query
rsResults.Open szSQLQuery, oConn
Set oDataGrid.ItemsSource = rsResults
rsResults.Close
oConn.close
Exit Sub
ErrorHandler:
If (rsResults.state = adoStateOpen) Then
rsResults.close
End If
If (oConn.state = adoStateOpen) Then
oConn.Close
End If
结束子
问题是我可以花很多时间尝试获取行数据。 我确实能够获得行索引(msgbox oDataGrid.SelectedIndex) 我使用的对象是“ System.Windows.Controls.DataGrid”
请告知, 谢谢, Yariv