0

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

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

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];

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

这就是我目前正在测试它的方式,但最后我希望查询中的所有列都显示在单独的标签中,我将如何做到这一点。或者有更好的方法吗?Gridview 是没有选择的。

提前致谢。

4

3 回答 3

1

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

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];
         }
     }

}
于 2012-12-28T07:33:58.240 回答
0

尝试像这样使用。

...
    while (dr.Read())
     {
            lbl3.Text += dr["order_no"].ToString() + "\n";
     }
...
于 2012-12-28T06:43:07.927 回答
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];
{
于 2012-12-28T09:47:02.720 回答