使用List填充标签C#

时间:2012-12-28 06:21:07

标签: c# visual-studio windows-ce

我正在VS 2005中创建一个设备应用程序。

我创建了一个名为“info”的List,并希望使用List中的值填充表单上的标签。这是我的代码:

public List<String> info = new List<String>();
int i = 0;


private void populateinfo()
    {
        conn.Open();
        string query;
        query = "select distinct dp.current_location_code,dci.dest_location_code,dps.order_no,dps.company_id_no,dps.no_of_full_cartons,dps.dc_grv_id_no,s.sku_code from dc_pallet_stock dps, dc_pallet dp,sku s , purch_order_carton_sku pocs , dc_crane_instruc dci where dp.pallet_id_no = dps.pallet_id_no and dps.order_no = pocs.order_no and dps.company_id_no = pocs.company_id_no and dps.carton_code = pocs.carton_code and s.sku_id_no = pocs.sku_id_no and s.company_id_no = dps.company_id_no and dp.pallet_id_no = '" + palletId + "' and dci.pallet_id_no(+) = dps.pallet_id_no";
        OracleCommand cmd = new OracleCommand(query, conn);
        OracleDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            this.info.Add(dr["order_no"].ToString());
        }
        dr.Close();
        conn.Close();
    }

private void frmInfo_Load(object sender, EventArgs e)
    {
        populateinfo();
        lbl3.Text = this.info[++i];
    {

我在lbl3.Text = this.info[++i];

收到错误
  

指定的参数超出了有效值的范围。参数名称:index。

这就是我目前正在测试它的方式,但最后我希望查询中的所有列都以单独的标签显示,我该怎么做。或者有更好的方法吗? Gridview是别无选择。

提前致谢。

3 个答案:

答案 0 :(得分:0)

试着像这样使用。

...
    while (dr.Read())
     {
            lbl3.Text += dr["order_no"].ToString() + "\n";
     }
...

答案 1 :(得分:0)

我可能会做的是创建一个标签数组或一个标签列表迭代它。下面是一个动态创建标签并将其添加到表单的示例。

public List<String> info = new List<String>();
public List<Label> labels = new List<Label>();

public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
     populateinfo();
     for (int i = 0; i < info.Count; i++)
     {
         labels.Add ( new Label(){Name="lbl"+i+1, Text=info[i], 
                                  Font = new Font("Arial",8),
                                  ForeColor= Color.Blue});

     }
     placelabels();
}

private void placelabels()
{
    int topvalue = 0;
    foreach (Label item in labels)
    {
        item.Left = 0;
        item.Top = topvalue;
        this.Controls.Add(item);
        topvalue += 20;

    }
}

将现有标签添加到列表

的方法
public List<String> info = new List<String>();
public List<Label> labels = new List<Label>();

public Form1()
{
    InitializeComponent();
    labels.Add(label1);
    labels.Add(label2);
    labels.Add(label3);
    labels.Add(label4);
    labels.Add(label5);
}

private void Form1_Load(object sender, EventArgs e)
{
     populateinfo();
     if (labels.Count > info.Count)
     {
         for (int i = 0; i < info.Count; i++)
         {
             labels[i].Text = info[i];
         }
     }
     else
     {
         for (int i = 0; i < labels.Count; i++)
         {
             labels[i].Text = info[i];
         }
     }

}

答案 2 :(得分:0)

我的选择查询中的

palletId不正确。请参阅构造函数:

public List<String> info = new List<String>();
int i = 0;

public frmInfo(string palletId)
    {
        InitializeComponent();
        this.palletId = palletId;
    }

private void populateinfo()
{
    conn.Open();
    string query;
    query = "select distinct dp.current_location_code,dci.dest_location_code,dps.order_no,dps.company_id_no,dps.no_of_full_cartons,dps.dc_grv_id_no,s.sku_code from dc_pallet_stock dps, dc_pallet dp,sku s , purch_order_carton_sku pocs , dc_crane_instruc dci where dp.pallet_id_no = dps.pallet_id_no and dps.order_no = pocs.order_no and dps.company_id_no = pocs.company_id_no and dps.carton_code = pocs.carton_code and s.sku_id_no = pocs.sku_id_no and s.company_id_no = dps.company_id_no and dp.pallet_id_no = '" + palletId + "' and dci.pallet_id_no(+) = dps.pallet_id_no";
    OracleCommand cmd = new OracleCommand(query, conn);
    OracleDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        this.info.Add(dr["order_no"].ToString());
    }
    dr.Close();
    conn.Close();
}

private void frmInfo_Load(object sender, EventArgs e)
{
    populateinfo();
    lbl3.Text = this.info[++i];
{