WPF将ListView绑定到Oracle数据源

时间:2010-08-03 18:03:23

标签: wpf vb.net oracle data-binding odp.net

以下是我正在处理的应用程序的XAML的一部分:

<ListView Name="lsvCustomerDetails" ItemsSource="{Binding myDataTable}">
   <ListView.View>
      <GridView>
         <GridViewColumn Header="Script Name" DisplayMemberBinding="{Binding ID}"/>
         <GridViewColumn Header="Status" DisplayMemberBinding="{Binding status}"/>
         <GridViewColumn Header="Date" DisplayMemberBinding="{Binding date}"/>
      </GridView>
   </ListView.View>
</ListView>

和相应的VB.NET:

Class Window1
   Public myDataTable As DataTable

   Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
      Dim objOracleConnection As New OracleConnection("Data Source=DB;User Id=user;Password=password;")
      Dim objOracleCommand As New OracleCommand
      Dim objOracleReader As OracleDataReader

      objOracleConnection.Open()

      objOracleCommand.Connection = objOracleConnection
      objOracleCommand.CommandText = "SELECT * FROM customers"
      objOracleReader = objOracleCommand.ExecuteReader()

      Using objOracleConnection
         Dim objOracleAdapter As OracleDataAdapter = New OracleDataAdapter(objOracleCommand.CommandText, objOracleConnection)
         objOracleAdapter.Fill(myDataTable)
      End Using
      lsvCustomerDetails.DataContext = myDataTable
      objOracleConnection.Close()
   End If
End Using

这段代码填写此表是否正确?如果没有,为什么?老实说,我已经找到了几个关于如何做这种事情的观点,其中一些在下面链接:

From switchonthecode
From csharpcorner
From Allen Mack's blog

1 个答案:

答案 0 :(得分:1)

你考虑过使用LINQ吗?它更容易......

将数据实体模型添加到项目中。阅读this,然后使用LINQ进行绑定。

类似的东西:

Dim Context as New myOracleEntityModel

myTable = Context.myCustomers.ToList

myItemSource = From i In myTable
               Select i.ID, i.status, i.date

lsvCustomerDetails.ItemSource = myItemSource

唯一的技巧是为Oracle加载数据实体模型。