【问题标题】:Reading a List<string>[] into a dynamic labels将 List<string>[] 读入动态标签
【发布时间】:2012-01-03 11:39:22
【问题描述】:

我在尝试将 List&lt;string[]&gt; 中的数据读入动态标签时遇到问题。

代码如下:

private void CuentasDeUsuarioForm_Load(object sender, EventArgs e)
        {
            List <string> [] cuentas = db.Select("SELECT * FROM cuentas_de_usuario");
            foreach(List<string> acc in cuentas)
            {
                for (int i = 0; i < acc.Count; i++)
                {
                    Label nuevoLabelCuenta = new Label { Text = acc[i] };
                }
            }
        }

有人知道我哪里做错了吗?我没有收到任何语法或编译错误,但它只是在表单中没有显示任何内容。

【问题讨论】:

    标签: c# .net winforms string list


    【解决方案1】:
    this.Controls.Add(nuevoLabelCuenta);
    

    (在内部 for 循环中)

    【讨论】:

    • 它有效,现在它显示第一列,但是当我尝试显示数据的所有列时出现 OutOfRangeException。有什么帮助吗?再次感谢!
    【解决方案2】:

    您应该像 Bas B 所说的那样为标签设置占位符。

    private void CuentasDeUsuarioForm_Load(object sender, EventArgs e)
        {
            List <string> [] cuentas = db.Select("SELECT * FROM cuentas_de_usuario");
            foreach(List<string> acc in cuentas)
            {
                for (int i = 0; i < acc.Count-1; i++)
                {
                    Label nuevoLabelCuenta = new Label { Text = acc[i] };                        
                    this.panel1.Controls.Add (nuevoLabelCuenta );
                }
            }
        }
    

    其中 Panel1 是您的占位符

    【讨论】:

    • 它可以在没有 -1 的情况下工作,但它显示一列,我想显示所有列(9 列)。有什么帮助吗?再次感谢:-)
    • 最简单的方法是将您的选择语句更改为 ("SELECT COL1+' '+COL2+' '+COL3" from cuentas_de_usuario")
    猜你喜欢
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多