2

As the title suggests, I'm trying to fill a ListBox with the results of a db query, though I'm not sure how to manage this. I've managed to populate a DataGrid from another query, but I'm not sure how to do the same with a ListBox. I'm sure it's simple, but I'm missing something! Similar overflow pages/google searches haven't yet yielded a clear answer. I'm using c# in a WPF app in visual studio.

In a separate class I have this:

public DataTable FillAllMenuItems()
{
     SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\DustCatcher\Documents\pizzeria.mdf;Integrated Security=True;Connect Timeout=30");
     SqlCommand cmd = new SqlCommand("SELECT name FROM MenuItems", con);
     SqlDataAdapter sda = new SqlDataAdapter(cmd);
     DataTable dt = new DataTable("Menu Items");
     sda.Fill(dt);
     return dt;
}

As an aside, I'm sure the above is messy so any pointers there would be great (do I need a using anywhere there?). On the relevant window cs file I have:

User user = User.GetInstance();
lstbxAllItems.ItemsSource = user.FillAllMenuItems().DefaultView;

Running the app, the ListBox has System.Data.DataRowView as many times as there are items in the database. Where am I going wrong?

3
  • Set the ValueMemberPath and DisplayMemberPath of your ListBox also I would recommend to use using statements for your SqlConnection, SqlCommand etc. Link for the ItemSource stackoverflow.com/questions/1790878/… Commented Dec 7, 2015 at 15:40
  • Displaymemberpath did it - thanks for your comment! Commented Dec 7, 2015 at 16:08
  • @user2946329 thanks for the lesson in brevity. Commented Dec 12, 2015 at 21:08

2 Answers 2

2

You need to set lstbxAllItems.DisplayMemberPath after lstbxAllItems.ItemsSource:

lstbxAllItems.DisplayMemberPath = "Name"; // Or any column you want to show in your ListBox

But as a better approach you can use ListView.ItemTemplate and Binding for this purpose. Something like this:

<ListBox Name="lstbxAllItems">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"></TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much for this, adding displaymemberpath did the trick! I'll read up a bit more about your suggested alternate method, as I'm not too sure how to implement it correctly!
1
private void BindListbox() 
{
    myListBox.DataSource = dt;
    myListBox.DisplayMemberPath = "Name";
}

2 Comments

Thanks for the answer, though I wanted to avoid further methods before I get bogged down in them all!
In WPF DisplayMember should be DisplayMemberPath. Please edit your answer and change it to DisplayMemberPath.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.