地址/更改C#中特定的ListView项 [英] Address/Change specific ListView item in c#
问题描述
我想知道如何在c#中的列表视图中处理和更改特定项目.我已经在表单中创建了一个列表视图,并实现了2列.我知道如何在运行时将项目添加到第一列,但是现在我想在运行时将项目添加到第二列(同一行)并进行更改!有什么想法怎么做?
Hi,
I''d like to know how to address and change a specific item in a listview in c#. I already created a listview in my form and implemented 2 columns. I know how to add an item to the first column at runtime, but now I want to add an item to the second column, same row, at runtime and change it! Any ideas how to do that?
Thanks in advance.
推荐答案
下面的示例演示如何通过指定行和列以及要设置的值来为列表视图设置值.
该示例将在需要时添加新行,但不会添加超过列表视图中定义的列:
The following example shows how you can set values for a list view by specifying row and column and a value to set.
The example will add a new line if required but will not add more columns than defined on the list view:
private void UpdateListViewValue(ListView listView, int row, int column, string newValue)
{
if (column >= listView.Columns.Count)
throw new InvalidOperationException("cannot modify outside column bounds");
ListViewItem item;
if (row < listView.Items.Count)
item = listView.Items[row];
else
{
item = new ListViewItem();
for (int i = 1; i < listView.Columns.Count; ++i)
item.SubItems.Add(new ListViewItem.ListViewSubItem());
listView.Items.Add(item);
}
item.SubItems[column].Text = newValue;
}
希望这会有所帮助,
Fredrik
Hope this helps,
Fredrik
列表视图的每个项目都是ListViewItem
类型.此类型的属性是SubItems,这是ListViewItems的集合,表示定义的每个列的所有值.
因此,在您的情况下,您想更改特定的列,因此可以进行以下操作:
Each item of a list view is ofListViewItem
type. A property of this type is SubItems, this is a collection of ListViewItems representing all the values for each column defined.
So in your case you want to change a specific column so give this a go:
// use the first item list item
ListViewItem myItem = listView1.Items[0];
myItem.SubItems[1].Text = "New Sub Item Value";
好的,在这种情况下,我选择了一个特定的项目,但是您可能想使用ListView的SelectedItems属性来检索您的LstViewItem.
希望这对您有帮助...
Ok in this case I have chosen a specific item, but you may want to use the SelectedItems property of ListView to retrieve your LstViewItem.
Hope this helps...
这篇关于地址/更改C#中特定的ListView项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!