开发者

WPF Bind a ListView to Oracle Data Source

Here's a part of XAML of the application I'm working on:

<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>

And the corresponding 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(myDataT开发者_开发技巧able)
      End Using
      lsvCustomerDetails.DataContext = myDataTable
      objOracleConnection.Close()
   End If
End Using

Is this code correct to fill this table? If not, why? Honestly, I've found several perspectives on how to do this sort of thing, some of which are linked below:

From switchonthecode

From csharpcorner

From Allen Mack's blog


Have you considered using LINQ? It is much easier ...

Add a Data Entity Model to your project. Read this and then use LINQ to do your binding.

Something like:

Dim Context as New myOracleEntityModel

myTable = Context.myCustomers.ToList

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

lsvCustomerDetails.ItemSource = myItemSource

The only trick is going to be loading the Data Entity model for Oracle.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜