【问题标题】:Copying data from a Winforms ListView从 Winforms ListView 复制数据
【发布时间】:2009-12-09 13:24:05
【问题描述】:

有没有办法从 .NET Winforms 中的 ListView 复制所选子项的文本?

【问题讨论】:

    标签: .net winforms listview


    【解决方案1】:

    ListView 控件内的每个项目都由ListViewItem 表示。 ListViewItem 有一个名为 SubItems 的属性,它从 ListView 控件中的第一列数据开始。

    要从列中复制数据,请获取选定的ListViewItem 并引用SubItems 属性中的Text 属性。

    例如,

    int theSelectedIndex = 0; // this should be the index of your selected item in the list
    int theSubItemIndex = 0; // this should be the index of the subitem whose text you want to copy
    
    ListViewItem lvItem = listView1.SelectedItems[theSelectedIndex];
    string text = lvItem.SubItems[theSubItemIndex].Text;
    

    【讨论】:

      【解决方案2】:

      除非我真的错过了一些基本的东西,否则你不能选择一个子项目。您可以选择第一列中的子项,也可以将 FullRowSelect 属性设置为 True。既不能帮助您确定用户可能感兴趣的子项,也无法猜测要复制到剪贴板的内容。

      使用 DataGridView 来解决这个问题。

      【讨论】:

      • 可以通过ListView.HitTest()函数判断鼠标点击时鼠标在哪个子项上
      • 是的,它看起来像一个 hack,如果对你有用的话,datagridview 是不错的选择。
      【解决方案3】:

      下面的代码sn -p会给你子项的值和子项的列名(如果你在创建子项的时候把col名存储在子项的tag值中的话。

      Point workItemsListViewLastHit;
      
      private void workItemsListView_MouseUp(object sender, MouseEventArgs e)
      {
          workItemsListViewLastHit = e.Location;
      }
      
      
      private void workItemsListView_DoubleClick(object sender, EventArgs e)
      {
          ListViewHitTestInfo HTI = workItemsListView.HitTest(workItemsListViewLastHit);
      
           if (HTI.Item != null)
           {
                     string field = HTI.SubItem.Tag as string;
                     string value = HTI.SubItem.Text;
           }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-19
        • 2011-05-04
        相关资源
        最近更新 更多