Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The closest answer I got was this Question.

https://social.msdn.microsoft.com/Forums/en-US/00336aa4-0895-4c4d-b1e5-380bbdffd961/display-linq-query-result-in-listview?forum=wpf[^]

so far either only Linq queries are getting displayed or nothing. I have tried a lot of different coding but can't seem to figure this out.

XML
public struct PIDData
        {
            public long PIdentity { get; set; }
            public string PName { get; set; }
            public string PPrice { get; set; }
            public string PSerial { get; set; }
            public string PDescription { get; set; }
            public string PType { get; set; }
            public string PPosition { get; set; }
            public Binary PImage { get; set; }
        }


       //Binding combobox
             using (DataClassesDataContext DC = new DataClassesDataContext())
           {
               cbItem.Items.Clear();
               foreach (tblProduct R in DC.tblProducts)
               cbItem.Items.Add(R);
               cbItem.DisplayMemberPath = "ProductName";
           }

        //Combobox Selection changed
           List<string> items = new List<string> { cbItem.SelectedItem.ToString() };

            IEnumerable<string> query = items.Where(item => item.ToString() != null);
            lvDataBinding.Items.Add(items);


            txt1.Text = items.ToString();

//Xaml
  <ListView Margin="338,20,1,16" Name="lvDataBinding">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding PName}">
                        <GridViewColumnHeader Tag="Product Name" Width="100">Product Name</GridViewColumnHeader>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
        <ComboBox x:Name="cbItem" HorizontalAlignment="Left" Margin="13,315,0,0" VerticalAlignment="Top" Width="321" SelectionChanged="cbItem_SelectionChanged"/>
Posted
Updated 15-Apr-15 23:01pm
v2
Comments
Michael_Davies 16-Apr-15 4:58am    
Show the code that is not working, otherwise just guessing.
RenaldoGG 16-Apr-15 5:03am    
sorry I am not sure where the problem is, if it is my binding of data.

This will help you out my friend:

XML
private void cmbSelectProduct_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    PIDData PID = (PIDData)cmbSelectProduct.SelectedItem;
    List<string> items = new List<string> { PID.Name };

    items.Where(item => item.ToString() != null).ToList().ForEach(i =>
    {
        lvAddProducts.Items.Add(i);
    });
}
 
Share this answer
 
Here is the issue:

Quote:
lvDataBinding.Items.Add(items)


The Items.Add method will create a new single item and will display the GetString().

You need to use Items.AddRange for groups of items, but not all collections support this.

A quick shortcut is to cast your query as a list and cann ForEach():
C#
...
//Combobox Selection changed
           List<string> items = new List<string> { cbItem.SelectedItem.ToString() };
 
            items.Where(item => item.ToString() != null).ToList().ForEach(i=>
            {
              lvDataBinding.Items.Add(i);
            }
 
            //What is this supposed to do?  collection.ToString() will, by default, return GetType().ToString().  That'll look like "System.Collections.List<string>" or similar
            txt1.Text = items.ToString();

            //You will need to get the string of a single item, or join a range of items strings.  There are many ways to do this
            string.Join(", ",items); //is one way of comma separating the values
...
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900