[Solved] c# How to show the total of values in a column in a listview? [closed]


Hi Mario to get your values in 1 column try this…i used a simple list
this will put values in first column

List<string> lst = new List<string>();
            lst.AddRange(new string[]{"one","two","three","four"});
            foreach(var value in lst)
            {
                listView1.Items.Add(value);
            }

if you want to put it in any other column try this

List<string> lst = new List<string>();
            lst.AddRange(new string[] { "one", "two", "three", "four" });
            int column = 1 ;//this could be some input like int.Parse(TextBox1.text)
            int row = 0;
            foreach (var value in lst)
            {
                if (!(column >= listView1.Columns.Count))//check to see if its not above column collection
                {
                    ListViewItem item = new ListViewItem();
                    listView1.Items.Add(item);
                    ListViewItem.ListViewSubItem lvsi = new ListViewItem.ListViewSubItem();
                    lvsi.Text = value.ToString();
                    listView1.Items[row].SubItems.Insert(column, lvsi);
                    row++;
                }

            }

ok Mario then you need this i think

private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            int value = 0;
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                value += int.Parse(listView1.Items[i].SubItems[e.Column].Text);
            }

            textBox1.Text = value.ToString();
        }

its the eventhandler for the columnclick event of listview,so when you
click the columnheader it will fire this logic….good coding

1

solved c# How to show the total of values in a column in a listview? [closed]