0

I'm trying to fill my listview in Windows forms with some sample data. I have a List of transactions whose properties are like following:

        public float NetAmount { get; set; }
        public DateTime TimeStamp { get; set; }
        public string TransactionID { get; set; }

This is how the so far sample data has been put to the listview like following:

 private void seedListView(List<Transactions> list)
        {
            List<string[]> _nesto = new List<string[]>();


            //Define
            var data = new[]
            {

                new []{"Lollipop", "392", "0.2"},
                new []{"KitKat", "518", "26.0"},
                new []{"Ice cream sandwich", "237", "9.0"},
                new []{"Jelly Bean", "375", "0.0"},
                new []{"Honeycomb", "408", "3.2"}
            };

            //Add
            foreach (string[] version in data)
            {
                var item = new ListViewItem(version);
                materialListView1.Items.Add(item);
            }
        }

With the data as data source for the listview I get the following output:

Lollipop  392 0.2
KitKat    518 26.2
HoneyComb 408 3.2

Now I need to do the same, but instead of that I'd like to have the listview filled with the items from the list that I passed into the function like this:

15,2 2016-02-01 125215FESSEFOKP

19.2 2016-02-01 125215FESSEFOKP

13.6 2016-02-01 125215FESSEFOKP

15.9 2016-02-01 125215FESSEFOKP

Where 15.2 is net amount , 2016-02-01 is the timestamp and 125215FESSEFOKP is TransactionID...

How could I do this dynamically ?

Edit: I've did it like this:

List<string[][]> _nesto = new List<string[][]>();
    string[][] data = null;
    foreach (var item in list)
    {
        data = new[] { new[] { item.NetAmount.ToString(), item.TimeStamp.ToString(), item.TransactionID.ToString() } };
        _nesto.Add(data);
    }
    ListViewItem it;
    foreach (var item in _nesto)
    {
        foreach (string[] version in item)
        {
            it = new ListViewItem(version);
            materialListView1.Items.Add(it);
        }
    }

Does anyone have a more cleaner solution than this one? This seems far too much code that I wrote for a simple thing like this :D

4
  • Instead of using string[] use object[] and that should do it. Commented Dec 9, 2016 at 18:17
  • Not sure what do you mean ? Commented Dec 9, 2016 at 18:19
  • see my answer below. creating this as arrays is a poor design choice in my opinion. this is why there are classes in programming. it represents an "object" and that is exactly what you are doing here with a transaction. it will make managing your code and making changes to it much easier versus dealing with it as an array Commented Dec 9, 2016 at 18:25
  • Yes I saw it, I've put a vote up for u! Thanks ! :) Commented Dec 9, 2016 at 18:27

3 Answers 3

1

I would do it like this

foreach (Transaction t  in list)
{
    var item = new ListViewItem(new string[]{ 
        t.NetAmount.ToString("N1"),
        t.TimeStamp.ToString("d"),
        t.TransactionID });
    materialListView1.Items.Add(item);
}
Sign up to request clarification or add additional context in comments.

4 Comments

hahahahah an overkill for my method! Thanks man! P.S. Take a look into my method that I wrote up there, yours is far far better !! :) Accepted ur answe as it is simply the best one !!
i see you have a transaction class (i missed that when i posted my answer below). you can basically ditch several of those lines by just adding that to an override ToString in your class. and just do t.ToString (assuming the format you want to present it in is always the same)
Yes, but in a listview you want to add the different properties to different columns. You won't get that with ToString. Note It's a ListView, not a ListBox.
@OlivierJacot-Descombes done , It didn't allow me to accept right away. Cheers mate ! :)
1

I think this is the way to do it:

  1. Create a class called transaction. it will have properties netamount, transaction id, timestamp. Create an override ToString method so that it will output the data how you want (as per your question).
  2. Have a list that will house all those transaction objects.
  3. You set the listview.ItemsSource equal to that list.
  4. listview.items.refresh();

it should populate your listview accordingly. this is the preferred way (at least to me) because you can just add and remove items from the list and you do not need to constantly be running foreach statements to make sure the data is correct in the listview.

2 Comments

@User987 i just want to point out a quick issue with the way you are dealing with your problem. by having a list of transactions and not binding it (via ItemsSource) to the listView. it will make handling the objects much more difficult. For example, if you add more methods to your transaction class or need to handle the transaction object, you can not do that directly from the listview you are using since you are adding it as a string array. Whereas if you use ItemsSource and override the ToString, you will have access to the actual transaction object and can use it. not sure if this is clear
@User987 For example, when you remove an string[] from your listview its going to still be in your list<transaction>. its going to get messy quickly
1

Here's what I mean:

         object[] data = new object[]
           {
            new object []{15.2 , 2016-02-01, "125215FESSEFOKP"},
            new object []{19.2, 2016-02-01, "125215FESSEFOKP"},
            new object []{19.2, 2016-02-01, "125215FESSEFOKP"},
            new object []{19.2, 2016-02-01, "125215FESSEFOKP"}
         };

    You won't be able to use anonymous type in this case.

Comments

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.