c# How to show the total of values in a column in

2019-09-21 20:52发布

I have to make a listview in WindowsForms, in which certain values must be added together, but I have no idea how to make this. Please can somebody help.

标签: c# listview add
1条回答
forever°为你锁心
2楼-- · 2019-09-21 21:04

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

查看更多
登录 后发表回答