1

I've got a table (tblManagerReports) that will hold my report information. This table consists of 4 fields:

  • ReportID - my Primary field
  • ReportName - the name of the report which will be displayed
  • SProc - the name of the stored procedure which needs to first run to calculate the data
  • SQLView - the name of the view that will populate the final datagrid.

ReportID and ReportName are populating a dropdown list, and what I want to do is determine which report is selected, query tblManagerReports to grab the SProc and SQLView fields, and then execute the appropriate stored procedure and populate the datagrid with the view. I figured this is the best way to make this app scalable, as all future reports will just need to be added to the table.

I know a "good question" on SO includes code, but I don't even know where to begin with this. Can anyone help me out? I've tried to do some googling but can't seem to find any reference to having multiple columns available in a combo box (not displaying multiple columns, but pulling them in so information in them can easily be retrieved), which I thought at first could solve some of this problem.

1 Answer 1

1

Define a class that should contain your row data

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

the use that class to create a 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);
        }

   }
}

finally use this List as DataSource for the combobox

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

You can retrieve your info using the Items collection as in this example

private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    MyReportData rpt = ComboBox1.SelectedItem as MyReportData;
    if(rpt != null)
    {
        ...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.