2012-12-28 170 views
0

我创造VS 2005使用列表来填充标签C#

我创建了一个名为“信息”列表中的设备的应用程序,并希望我的形式从列表中值上填充标签。这是我的代码:

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是没有选择的。

在此先感谢。

+0

您是否正在填充'info'数组?你不在你向我们展示的代码中。这就是为什么你得到超出范围的例外。 – hetelek

+0

@hetelek这只是一个错字。我已纠正它。仍然得到相同的错误 –

回答

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

尝试使用这样的。

... 
    while (dr.Read()) 
    { 
      lbl3.Text += dr["order_no"].ToString() + "\n"; 
    } 
... 
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]; 
     } 
    } 

}