none
Set datasource to collection of property RRS feed

  • Question

  • I have a List<Info> where Info is property class including properties say name,address.

    Now i have to bind names to ListBox. How can i set that ? 

    If i have to iterate and create DataTable of names or any easy method exist??

    class Info

    {

    string name {get ; set ; }

    string address {get ; set ; }

    }


    Deepak Tripathi

    Thursday, July 5, 2012 6:02 PM

Answers

  • If my interpretation is correct, you just want to bind Name item to ListBox from List<Info>.

    So you may try this in WPF;

    XAML

    <ListBox x:Name="NameList" Margin="..." HorizontalAlignment="..." Width="..." />

    C#

    List<info> lst = getList(); //get item list from somewhere
    NameList.ItemsSource = lst.Select(ii => ii.Name);

    Hope this helps!




    • Edited by TinMgAye Friday, July 6, 2012 5:46 AM
    • Marked as answer by tdmca Friday, July 6, 2012 9:38 AM
    Friday, July 6, 2012 5:46 AM
  • Hi Deepak,

    You can use ItemSource to bind the object list to DataGrid but you need to set True for AutoGenerateColumn if you doing dynamic binding.

    XAML

    <DataGrid x:Name="ItemGrid" AutoGenerateColumns="True" ...... />

    C#

    List<info> lst = getList(); //get item list from somewhere
    ItemGrid.ItemsSource = lst;
    Hope this helps!

    • Marked as answer by tdmca Friday, July 6, 2012 5:03 PM
    Friday, July 6, 2012 12:21 PM

All replies

  • Hi tdmca;

    Lets say that you have listBox1 and your list of Info  instance is called myInfoList then to display the name property in the list box do the following.

    listBox1.DataSource = myInfoList;
    listBox1.DisplayMember = "name";

    That should be it.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, July 5, 2012 7:56 PM
  • hi Fernando,

    thanx for reply, but i am using WPF ListBox, can you please help me out, how can i do the same in WPF ListBox


    Deepak Tripathi

    Friday, July 6, 2012 4:51 AM
  • If my interpretation is correct, you just want to bind Name item to ListBox from List<Info>.

    So you may try this in WPF;

    XAML

    <ListBox x:Name="NameList" Margin="..." HorizontalAlignment="..." Width="..." />

    C#

    List<info> lst = getList(); //get item list from somewhere
    NameList.ItemsSource = lst.Select(ii => ii.Name);

    Hope this helps!




    • Edited by TinMgAye Friday, July 6, 2012 5:46 AM
    • Marked as answer by tdmca Friday, July 6, 2012 9:38 AM
    Friday, July 6, 2012 5:46 AM
  • Thank you TinMgAye, This solved my problem

    I very beginner in lambda expression.

    If i have to bind this List<info> to DataGrid to show all properties as Columns

    then what will be expression or syntax

    Please tell me, i will be very thankful


    Deepak Tripathi

    Friday, July 6, 2012 10:05 AM
  • Hi Deepak,

    You can use ItemSource to bind the object list to DataGrid but you need to set True for AutoGenerateColumn if you doing dynamic binding.

    XAML

    <DataGrid x:Name="ItemGrid" AutoGenerateColumns="True" ...... />

    C#

    List<info> lst = getList(); //get item list from somewhere
    ItemGrid.ItemsSource = lst;
    Hope this helps!

    • Marked as answer by tdmca Friday, July 6, 2012 5:03 PM
    Friday, July 6, 2012 12:21 PM
  • <DataGrid x:Name="ItemGrid" AutoGenerateColumns="True" ...... />
    List<info> lst = getList(); //get item list from somewhere
    ItemGrid.ItemsSource = lst;

    Thanks for your response, but it is not working.

    i have tested datagrid is working fine with DataSet with autogenerate column.

    It is showing blank DataGrid with no structure.


    Deepak Tripathi

    Friday, July 6, 2012 2:00 PM
  • I would suggest you to go breakpoint and debug the code to make sure you are passing correct object list.

    Share your code with us, so other expert can advice too.

    Friday, July 6, 2012 2:46 PM
  • ok thanx it is done, may be some temporary issue

    thanx for valuable support


    Deepak Tripathi

    Friday, July 6, 2012 5:03 PM
  • hi TinMgAye

    how can i customize my ListBox to show more than one value like name and address both.

    and what change if any i have to do in c# code.


    Deepak Tripathi

    Monday, July 9, 2012 9:46 AM
  • Hi Deepak,

    I would suggest that you should post a new thread against this question, since this thread is not popular now.

    Best Regards,


    Tony Xiao [MSFT]
    MSDN Community Support | Feedback to us

    Monday, July 16, 2012 11:52 AM
    Moderator