You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在VS Code中使用C#,如何提取ListView勾选行的首列内容?

提取ListView勾选行的第一列内容

直接遍历ListView的所有项,筛选出复选框被勾选的条目,然后提取第一列内容即可,核心代码如下:

// 假设你的ListView控件实例名为listView1
List<string> checkedFirstColumnTexts = new List<string>();

foreach (ListViewItem item in listView1.Items)
{
    if (item.Checked)
    {
        // 第一列内容对应ListViewItem的Text属性,也可以用item.SubItems[0].Text(效果一致)
        checkedFirstColumnTexts.Add(item.Text);
    }
}

// 示例:输出所有提取到的内容
foreach (string text in checkedFirstColumnTexts)
{
    Console.WriteLine(text);
}

注意事项

  • 确认你的ListView控件的CheckBoxes属性已设置为true(你提到已添加复选框,此步可忽略)
  • 不要混淆Checked和Selected:Checked是复选框勾选状态,Selected是条目高亮选中状态,两者互不关联
  • 如果使用了虚拟模式(VirtualMode = true),不能直接遍历Items集合,需要通过VirtualListSize遍历索引,再获取对应项的内容:
    List<string> checkedTexts = new List<string>();
    for (int i = 0; i < listView1.VirtualListSize; i++)
    {
        ListViewItem item = listView1.Items[i];
        if (item.Checked)
        {
            checkedTexts.Add(item.Text);
        }
    }
    

内容的提问来源于stack exchange,提问作者JLPANIC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 17:31:15