-
Oct 17th, 2010, 08:28 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Databinding to a WPF listbox
Hi everyone,
I have a listbox on a window that i simply want to fill with conditional data from a database that scans a customer database and counts off the number of entries that don't have an email address associated with them and displays the company name in a listbox - here's the code:
Code:
Dim con As New SqlClient.SqlConnection
Dim ds As New DataSet
Dim da As SqlClient.SqlDataAdapter
Dim sql As String
con.ConnectionString = connectstring
con.Open()
sql = "select Company_Name FROM Customer where Sales_Rep = '" & getsalesrepname & "' AND (Email IS NULL OR Email = '')"
da = New SqlClient.SqlDataAdapter(sql, con)
da.Fill(ds, "Customer")
lbCompaniesMissing.ItemsSource = ds.Tables(0).Rows()
lbCompaniesMissing.DisplayMemberPath = "Company_Name"
con.Close()
The problem is that it appears to fill the listbox with the correct number of rows, but they are blank - if i highlight the first 'blank' entry and count them off by simply scrolling down, it gives me the expected 59 rows that the above query returns in my case - just no text is displayed in the listbox.....
any ideas would be appreciated...as usual i'll keep hunting for a solution myself and if i find it i'll post it here.
-
Oct 17th, 2010, 08:39 AM
#2
Thread Starter
Hyperactive Member
Re: Databinding to a WPF listbox
oh and by the way, 'con' is defined further up in the code, i just didn't include it here in the snippet (oh and also, this code works perfectly in the winforms app that is being converted to this wpf project) - i'm focused on the .itemssource statement - do i have the syntax wrong for wpf listboxes??
-
Oct 17th, 2010, 08:58 AM
#3
Thread Starter
Hyperactive Member
Re: Databinding to a WPF listbox
here's some more behavior - if i change the itemssource to this:
Code:
lbCompaniesMissing.ItemsSource = ds.Tables()
The first correct entry shows up in the listbox - but only that entry, not the 58 additional entries i'm expecting. If i change the itemssource to this:
Code:
lbCompaniesMissing.ItemsSource = ds.Tables(0).Rows()
my 59 rows are present in the listbox, but display nothing......
-
Oct 17th, 2010, 09:08 AM
#4
Thread Starter
Hyperactive Member
Re: Databinding to a WPF listbox
And one other change....if i do this:
Code:
lbCompaniesMissing.ItemsSource = ds.Tables(0)
i get {"Unable to cast object of type 'System.Data.DataTable' to type 'System.Collections.IEnumerable'."}
-
Oct 17th, 2010, 12:19 PM
#5
Thread Starter
Hyperactive Member
Re: Databinding to a WPF listbox
okey dokey, figured it out - you need to set the datacontext, not the itemssource in the codebehind, then you set the itemssource to the dataset in the xaml - here's the codebehind:
Code:
Dim con As New SqlClient.SqlConnection
Dim ds As New DataSet
Dim da As SqlClient.SqlDataAdapter
Dim sql As String
con.ConnectionString = connectstring
con.Open()
sql = "select Company_Name FROM Customer where Sales_Rep = '" & getsalesrepname & "' AND (Email IS NULL OR Email = '')"
da = New SqlClient.SqlDataAdapter(sql, con)
da.Fill(ds, "Customer")
lbCompaniesMissing.DataContext = ds
lbCompaniesMissing.DisplayMemberPath = "Company_Name"
con.Close()
and here's the xaml for the listbox itself:
Code:
<ListBox Height="199" HorizontalAlignment="Left" Margin="273,12,0,0" Name="lbCompaniesMissing" VerticalAlignment="Top" Width="306" BorderBrush="Black" Background="White" ItemsSource="{Binding Tables[0]}"/>
The key is the ItemsSource="{Binding Tables[0]}" - i think i'll also add a sort as well because the list isn't alphabetical at the moment
hopefully this can help others who come across the same problem.....
-
Oct 17th, 2010, 12:29 PM
#6
Thread Starter
Hyperactive Member
Re: [RESOLVED] Databinding to a WPF listbox
here's the code for the codebehind to do the sort by the way:
Code:
lbCompaniesMissing.Items.SortDescriptions.Add(New ComponentModel.SortDescription("Company_Name", System.ComponentModel.ListSortDirection.Ascending))
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|