2014-01-06 26 views
1

我有一個表(tblManagerReports),它將保存我的報告信息。此表由4個領域:從SQL Server表中檢索數據用於進一步計算

  • ReportID - 我的主要領域
  • REPORTNAME - 需要先運行到 存儲過程的名稱 - 這將顯示
  • 存儲過程的報表的名稱計算數據
  • SQLView - 將填充最終數據網格的視圖的名稱。

ReportID和REPORTNAME被填充一個下拉列表,我想要做的就是確定選擇哪個報告,查詢tblManagerReports搶存儲過程和SQLView字段,然後執行相應的存儲過程和填充使用DataGrid風景。我認爲這是使該應用程序可擴展的最佳方式,因爲所有將來的報告都需要添加到表格中。

我知道關於SO的一個「好問題」包括代碼,但我甚至不知道從哪裏開始。誰能幫我嗎?我試圖做一些谷歌搜索,但似乎無法找到任何提及在組合框中有多個列(不是顯示多列,但拉他們在他們的信息可以很容易地檢索),其中我起初想到的可以解決這個問題。

回答

1

定義應該包含您的行數據

public class ReportData 
{ 
    public int ReportID; 
    public string ReportName; 
    public string SProc; 
    public string SQLView; 
} 

使用這個類來創建一個List<ReportData>

List<ReportData> myReportData = new List<ReportData>(); 
using(SqlConnection con = new SqlConnection(...)) 
using(SqlCommand cmd = new SqlCommand("SELECT * from tblManagerReports", con)) 
{ 
    con.Open(); 
    using(SqlDataReader reader = cmd.ExecuteReader()) 
    { 
     while(reader.Read()) 
     { 
      ReportData rpt = new ReportData(); 
      rpt.ReportID = Convert.ToInt32(reader[0]); 
      rpt.ReportName = reader[1].ToString(); 
      rpt.SProc = reader[2].ToString(); 
      rpt.SQLView = reader[3].ToString(); 
      myReportData.Add(rpt); 
     } 

    } 
} 

最後使用這個列表作爲數據源的組合框

comboBox1.DataSource = myReportData; 
comboBox1.ValueMember = "ReportID"; 
comboBox1.DisplayMember = "ReportName"; 

您可以像這樣使用Items集合檢索您的信息示例

private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e) 
{ 
    MyReportData rpt = ComboBox1.SelectedItem as MyReportData; 
    if(rpt != null) 
    { 
     ... 
    } 
} 
相關問題