且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c#如何在列表视图的一列中显示值的总和?

更新时间:2023-02-10 12:40:59

马里奥(Mario)将您的值显示在1列中,尝试一下...我使用了一个简单的列表 这会将值放在第一列

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();
        }

为listview的columnclick事件提供事件处理程序,因此当您 单击columnheader将会触发此逻辑....良好的编码

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