【发布时间】:2015-05-16 05:48:05
【问题描述】:
我一直在尝试找到一种方法来从选定的ListView 行中读取数据,并在其受人尊敬的TextBox 中显示每个值以便于编辑。
第一个也是最简单的方法是这样的:
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 个左右会变得非常乏味。
我想出的解决方案是在我的用户控件中获取所有 TextBox 控件,如下所示:
foreach (Control c in this.Controls)
{
foreach (Control childc in c.Controls)
{
if (childc is TextBox)
{
}
}
}
然后我需要循环选定的ListView 行列标题。如果他们的列标题与 TextBox.Tag 匹配,则在他们尊重的 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 行和每个列标题。
【问题讨论】:
标签: c# winforms listview columnheader