2010-08-03 43 views
0

這裏是XAML的應用我工作的一部分:WPF綁定一個ListView到Oracle數據源

<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

你有沒有考慮過使用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數據實體模型。

+0

這確實比我所擁有的更清潔和更好。我會研究它。 – Riddari 2011-07-11 19:47:58