I am new here so forgive me if this is the wrong section for my question. My problem is that I can't populate a datgrid with the contents of a dataset. Having searched for the last few days to try and resolve this, I have given up and thought I wou开发者_如何学Cld try here. I am using Vs 2010 VB.Net and it's a WPF application As you can tell from the code I am very new to this and any suggestions for improvement will be most welcome. I can see that the dataset is populated ok via the visualizer, I have a datagrid 'dgBOM' which I want to display the results. A popular solution in my searches was to add '.DataSource' but when I type the '.' after dgBOM the Datasource option is not there.
Any help would be much appreciated.
Thank you.
Code:
Public Sub Connect()
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim ds As DataSet
Dim da As OleDbDataAdapter
Dim dt As DataTable
Dim drow As DataRow
Dim PartColumn As DataColumn
Dim CostColumn As DataColumn
dt = New DataTable("BOM")
ds = New DataSet("BOM")
PartColumn = New DataColumn("PartNo")
CostColumn = New DataColumn("Cost")
da = Nothing
cn = Nothing
dr = Nothing
Try
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=\\Ls-wtgl834\share\Adrian\PriceLists\WAP-PriceLists.mdb;User ID=Admin")
cn.Open()
cmd = New OleDbCommand("select * from Inventory", cn)
dr = cmd.ExecuteReader
da = New OleDbDataAdapter(cmd)
dt.Columns.Add(PartColumn)
dt.Columns.Add(CostColumn)
While dr.Read()
If dr(0) = FBladeExtNo Then
FrontBladeCost = dr(1)
drow = dt.NewRow()
drow("PartNo") = FBladeExtNo
drow("Cost") = FrontBladeCost
dt.Rows.Add(drow)
ds.Tables.Add(dt)
Exit While
End If
End While
dgBOM.ItemsSource = ds
Catch
End Try
dr.Close()
cn.Close()
End Sub
I have given up on DataReader and all that. After writing a program a couple years ago where I had to provide all of the logic for change tracking and concurrency and relationship tracking and everything I threw up my hands because it is so time intensive.
Nowadays I use an O/RM framework; that is Object/Relation Mapping. Two of the biggies right now are Entity Framework (my personal favored framework) and NHibernate (originally written for Java but ported for .NET). These frameworks allow you to map database tables to .NET classes. Either ones you write yourself or ones generated by the tool (in Entity Frameworks case.
EF in Visual Studio 2010 allows you to do Model-First and Database-First development. Model-First allows you to create a diagram of how you want your classes to relate to each other (e.g. A Customer class that has a collection of Addresses, Phone Numbers and Events) then generates a database to store that data. Database-First is the opposite. You create your database, and EF tries to make a model that reflects the Database structure. It takes care of connection string creation, entity change tracking and concurrency, and it presents the data in nice objects that in my opinion are generated pretty darned fast. Personally I have found EFs data retrieval faster than my own hand executed data retrieval and object population.
WPF, with it's databinding capabilities, is geared more toward these dataobjects (here after I will just call them entities) than to DataReaders and DataAdapters. If you want to dive right in, here is a little Microsoft video on getting started with EF. Also be sure to check out WindowsClient.net as they have some good stuff there too.
Later on, once you get the hang of EntityFramework, check out the MVVM design pattern.
精彩评论