定義應該包含您的行數據
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)
{
...
}
}