0

So i have this Dictionary:

 Dictionary<string, double> _statistics;

Ans i want to add this Dictionary into my ListView with 2 columns:

DataTable table = new DataTable();
table.Columns.Add("Name");
table.Columns.Add("Percent");

foreach (KeyValuePair<string, double> item in _statistics)
    table.Rows.Add(item.Key, item.Value);
listView.ItemsSource = table;

And got an error:

Cannot implicitly convert type 'System.Data.DataTable' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)

Edit

I also try this:

public class MyItem
{
    public string Name{ get; set; }

    public double Percentage { get; set; }
}

        var gridView = new GridView();
        ipStatilistViewsticslistView.View = gridView;
        gridView.Columns.Add(new GridViewColumn
        {
            Header = "Name",
            DisplayMemberBinding = new Binding("Name")
        });
        gridView.Columns.Add(new GridViewColumn
        {
            Header = "Percent",
            DisplayMemberBinding = new Binding("Percent")
        });

        foreach (KeyValuePair<string, double> item in _statistics)
            listView.Items.Add(new MyItem { Name = item.Key, Percentage = item .Value}); 

And in this case all i can see is my 2 columns heards.

4
  • Why you want to use a DataTable? Commented Sep 26, 2015 at 10:14
  • I don't care what to use, i only want to populate my ListView with my Dictionary. Commented Sep 26, 2015 at 10:20
  • Then you can just write listView.ItemsSource = _statistics. ListView.ItemSource will work with anything that implement the IEnumerable interface. Commented Sep 26, 2015 at 10:23
  • Thats works fine i can see my value inside [ ] braces and also i need the column header, width... Commented Sep 26, 2015 at 10:25

1 Answer 1

1

ItemsControl.ItemsSource is of IEnumerable type and DataTable does not implement that interface. You need to use DataView instead

listView.ItemsSource = table.DefaultView;

or

listView.ItemsSource = new DataView(table);

EDIT

As for your second example you set view of ipStatilistViewsticslistView and populate listView with items. I'm guessing it's not the same ListView. Also you bind Percent column whilst name of the property is Percentage

Sign up to request clarification or add additional context in comments.

4 Comments

All i can see now is one column with System.Data.DataRowView insid (i try both options).
do you use default or custom ItemTemplate in the ListView?
In my XAML i only declare the ListView, no other changes was made.
Check my edit. There is also a problem in your second attempt. With these fixes second attempt should work. However I would set ItemSource to instance of ObservableCollection<MyItem> and keep adding items to that collection

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.