我有一个表(tblManagerReports),它将保存我的报告信息。该表由4个字段组成:
ReportID和ReportName正在填充下拉列表,我想要做的是确定选择了哪个报表,查询tblManagerReports以获取SProc和SQLView字段,然后执行相应的存储过程并使用视图填充数据网格。我认为这是使这个应用程序可扩展的最佳方式,因为所有未来的报告都只需要添加到表中。
我知道关于SO的“好问题”包括代码,但我甚至不知道从哪里开始。谁能帮我吗?我试过做一些谷歌搜索,但似乎找不到任何关于在组合框中有多个列可用的参考(不是显示多列,但是将它们拉入其中,因此其中的信息很容易就可以了我认为起初可以解决这个问题。
答案 0 :(得分: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);
}
}
}
最后将此List用作组合框的数据源
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)
{
...
}
}