c#如何在列表视图的一列中显示值的总和? [英] c# How to show the total of values in a column in a listview?

查看:115
本文介绍了c#如何在列表视图的一列中显示值的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在WindowsForms中创建一个列表视图,其中必须将某些值添加在一起,但是我不知道如何实现此目的.请有人帮忙.

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.

推荐答案

马里奥(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

这篇关于c#如何在列表视图的一列中显示值的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆