如何阅读ListView列标题及其值? [英] How can I read ListView Column Headers and their values?

查看:438
本文介绍了如何阅读ListView列标题及其值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试寻找一种方法来从选定的ListView行中读取数据,并在其受尊重的TextBox中显示每个值,以便于编辑.

I've been trying to find out a way to read data from the selected ListView row and display each value in their respected TextBox for easy editing.

第一种也是最简单的方法如下:

The first and easiest way would be something like this:

ListViewItem item = listView1.SelectedItems[0];

buyCount_txtBox.Text = item.SubItems[1].Text;
buyPrice_txtBox.Text = item.SubItems[2].Text;
sellPrice_txtBox.Text = item.SubItems[3].Text;

该代码没有什么问题,但是我有大约40个或更多的TextBoxes应该显示数据.对所有40个左右的代码进行编码将变得非常乏味.

There is nothing wrong with that code but I have around 40 or more TextBoxes that should display data. Coding all 40 or so would become very tedious.

我想出的解决方案是像这样在用户控件中获取所有TextBox控件:

The solution I've come up with, is to get all TextBox Controls in my User Control like so:

    foreach (Control c in this.Controls)
    {
        foreach (Control childc in c.Controls)
        {
            if (childc is TextBox)
            {
            }
        }
    }

然后,我需要循环选择的ListView行列标题.如果其列标题与TextBox.Tag匹配,则在其受尊重的TextBox中显示列值.

Then I need to loop the selected ListView row column headers. If their column header matches TextBox.Tag then display the column value in their respected TextBox.

最终代码如下:

    foreach (Control c in this.Controls)
    {
        foreach (Control childc in c.Controls)
        {

          // Needs another loop for the selected ListView Row

            if (childc is TextBox && ColumnHeader == childc.Tag)
            {
               // Display Values
            }
        }
    }

所以我的问题是:如何循环遍历所选的ListView行和每个列标题.

So then my question would be: How can I loop through the selected ListView Row and each column header.

推荐答案

像这样简单地遍历ColumnHeaders:

foreach(  ColumnHeader  lvch  in listView1.Columns)
{
    if (lvch.Text == textBox.Tag) ; // either check on the header text..
    if (lvch.Name == textBox.Tag) ; // or on its Name..
    if (lvch.Tag  == textBox.Tag) ; // or even on its Tag
}

但是,即使有效,循环遍历TextBoxes的方式也不是很好.我建议您将每个参与的TextBoxes添加到List<TextBox>中.是的,这意味着要添加40个项目,但是您可以像这样使用AddRange:

However the way you loop over your TextBoxes is not exactly nice even if it works. I suggest that you add each of the participating TextBoxes into a List<TextBox>. Yes, that means to add 40 items, but you can use AddRange maybe like this:

填写myBoxes列表:

To fill a list myBoxes:

List<TextBox> myBoxes = new List<TextBox>()

public Form1()
{
    InitializeComponent();
    //..
    myBoxes.AddRange(new[] {textBox1, textBox2, textBox3});
}

或者,如果您真的想避免使用AddRange并且保持动态,还可以编写一个小的递归.:

Or, if you really want to avoid the AddRangeand also stay dynamic, you can also write a tiny recursion..:

private void CollectTBs(Control ctl, List<TextBox> myBoxes)
{
    if (ctl is TextBox) myBoxes.Add(ctl as TextBox);
    foreach (Control c in ctl.Controls) CollectTBs(c, myBoxes);
}

现在,您的最终循环变得纤细而快速:

Now your final loop is slim and fast:

foreach(  ColumnHeader  lvch  in listView1.Columns)
{
    foreach (TextBox textBox in myBoxes)
        if (lvch.Tag == textBox.Tag)  // pick you comparison!
            textBox.Text = lvch.Text;
}

更新:由于您实际上想要SubItem值,因此解决方案可能如下所示:

Update: since you actually want the SubItem values the solution could look like this:

ListViewItem lvi = listView1.SelectedItems[0];
foreach (ListViewItem.ListViewSubItem lvsu in  lvi.SubItems)
    foreach (TextBox textBox in myBoxes)
       if (lvsu.Tag == textBox.Tag)  textBox.Text = lvsu.Text;

这篇关于如何阅读ListView列标题及其值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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