2012-12-28 121 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]; 
     } 
    } 

}