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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答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();
}
its the eventhandler for the columnclick event of listview,so when you click the columnheader it will fire this logic....good coding