How to DataBind ComboBox with TableAdapter?

joe_pool_is

Well-known member
Joined
Jan 18, 2004
Messages
451
Location
Texas
I have a DataGridView that successfully populates using our SQL 2000 Server (Using VS 2005).

Whenever the Form loads, I need to populate ComboBox Items with Column options from the EmployeeInfo table.

I have something that runs below, but trying to populate the ComboBoxes throws "Complex DataBinding accepts as a data source either an IList or an IListSource."

What am I doing wrong?
Code:
    public void DataLoad() {
      DataSet ds;
      string sqlCmd;
      string sqlFmt = "SELECT {0} FROM dbo.EmployeeInfo {1}";
      // taEmpInfo is the TableAdapter that links to the DataGridView
      SqlConnection conn = taEmpInfo.Connection;
      SqlDataAdapter da;
      string strErr = "";
      try {
        ds = new DataSet("EmployeeInfo");
        sqlCmd = string.Format(sqlFmt, "*", "Order By [LASTNAME] Asc");
        try { // load full list of employees into the DataGridView dgEmpInfo:
          da = new SqlDataAdapter(sqlCmd, conn);
          da.Fill(dsEmployees, "EmployeeInfo");
          dgEmpInfo.DataSource = dsEmployees.Tables["EmployeeInfo"].DefaultView;
        } catch (Exception e) {
          strErr += "Error Loading the full list:\n" + e.Message + "\n";
        }
        sqlCmd = string.Format(sqlFmt, "DISTINCT [GROUP]", "Order By [GROUP] Asc");
        try { // Fill ComboBox for Groups:
          da = new SqlDataAdapter(sqlCmd, conn);
          da.Fill(ds, "EmployeeInfo");
          cboGroup.DataSource = ds.Tables["EmployeeInfo"].Columns[0];
        } catch (Exception e) {
          strErr += "Error Loading the groups:\n" + e.Message + "\n";
        }
        sqlCmd = string.Format(sqlFmt, "DISTINCT [DEPT]", "Order By [DEPT] Asc");
        try { // Fill ComboBox for Depts:
          da = new SqlDataAdapter(sqlCmd, conn);
          da.Fill(ds, "EmployeeInfo");
          cboDept.DataSource = ds.Tables["EmployeeInfo"].Columns[0];
        } catch (Exception e) {
          strErr += "Error Loading the departments:\n" + e.Message + "\n";
        }
        sqlCmd = string.Format(sqlFmt, "DISTINCT [JOBTITLE]", "Order By [JOBTITLE] Asc");
        try { // Fill ComboBox for JobTitles:
          da = new SqlDataAdapter(sqlCmd, conn);
          da.Fill(ds, "EmployeeInfo");
          cboTitle.DataSource = ds.Tables["EmployeeInfo"].Columns[0];
        } catch (Exception e) {
          strErr += "Error Loading the job titles:\n" + e.Message + "\n";
        }
        sqlCmd = string.Format(sqlFmt, "[LASTNAME] + ,  [FIRSTNAME] AS FullName", "WHERE [JOBTITLE]=MANAGER");
        try { // Fill ComboBox for Managers (Lastname, Firstname):
          da = new SqlDataAdapter(sqlCmd, conn);
          da.Fill(ds, "EmployeeInfo");
          cboMgr.DataSource = ds.Tables["EmployeeInfo"].Columns[0];
        } catch (Exception e) {
          strErr += "Error Loading the Managers:\n" + e.Message + "\n";
        }
      } catch (Exception e) {
        strErr += "Error Opening DataSet:\n" + e.Message + "\n";
      }
      if (strErr != "") {
        Console.WriteLine(strErr);
      }
    }
 
Solved!

Anyone care to see the results?

If you find this code helpful, please post a reply letting me know:
Code:
    public void DataLoad() {
      string sqlCmd;
      string sqlFmt = "SELECT {0} FROM dbo.EmployeeInfo {1}";
      // taEmpInfo is the TableAdapter that links to the DataGridView
      SqlConnection conn = taEmpInfo.Connection;
      SqlDataAdapter da;
      string strErr = "";
      try {
        sqlCmd = string.Format(sqlFmt, "*", "Order By [LASTNAME] Asc");
        try { // load full list of employees into the DataGridView dgEmpInfo:
          da = new SqlDataAdapter(sqlCmd, conn);
          da.Fill(dsEmployees, "EmployeeInfo");
          dgEmpInfo.DataSource = dsEmployees.Tables["EmployeeInfo"].DefaultView;
        } catch (Exception e) {
          strErr += "Error Loading the full list:\n" + e.Message + "\n";
        }
        sqlCmd = string.Format(sqlFmt, "DISTINCT [GROUP]", "Order By [GROUP] Asc");
        try { // Fill ComboBox for Groups:
          DataSet ds = new DataSet("EmployeeInfo");
          da = new SqlDataAdapter(sqlCmd, conn);
          da.Fill(ds, "EmployeeInfo");
          foreach (DataRow dr in ds.Tables["EmployeeInfo"].Rows) {
            string strVal = dr.ItemArray.GetValue(0).ToString().Trim();
            if (strVal != "") cboGroup.Items.Add(strVal);
          }
          //cboGroup.DataSource = ds.Tables["EmployeeInfo"].Columns[0];
        } catch (Exception e) {
          strErr += "Error Loading the groups:\n" + e.Message + "\n";
        }
        sqlCmd = string.Format(sqlFmt, "DISTINCT [DEPT]", "Order By [DEPT] Asc");
        try { // Fill ComboBox for Depts:
          DataSet ds = new DataSet("EmployeeInfo");
          da = new SqlDataAdapter(sqlCmd, conn);
          da.Fill(ds, "EmployeeInfo");
          foreach (DataRow dr in ds.Tables["EmployeeInfo"].Rows) {
            string strVal = dr.ItemArray.GetValue(0).ToString().Trim();
            if (strVal != "") cboDept.Items.Add(strVal);
          }
          //cboDept.DataSource = ds.Tables["EmployeeInfo"].Columns[0];
        } catch (Exception e) {
          strErr += "Error Loading the departments:\n" + e.Message + "\n";
        }
        sqlCmd = string.Format(sqlFmt, "DISTINCT [JOBTITLE]", "Order By [JOBTITLE] Asc");
        try { // Fill ComboBox for JobTitles:
          DataSet ds = new DataSet("EmployeeInfo");
          da = new SqlDataAdapter(sqlCmd, conn);
          da.Fill(ds, "EmployeeInfo");
          foreach (DataRow dr in ds.Tables["EmployeeInfo"].Rows) {
            string strVal = dr.ItemArray.GetValue(0).ToString().Trim();
            if (strVal != "") cboTitle.Items.Add(strVal);
          }
          //cboTitle.DataSource = ds.Tables["EmployeeInfo"].Columns[0];
        } catch (Exception e) {
          strErr += "Error Loading the job titles:\n" + e.Message + "\n";
        }
        sqlCmd = string.Format(sqlFmt, "[LASTNAME] + ,  + [FIRSTNAME] AS FullName", "WHERE [JOBTITLE]=MANAGER Order By FullName Asc");
        try { // Fill ComboBox for Managers (Lastname, Firstname):
          DataSet ds = new DataSet("EmployeeInfo");
          da = new SqlDataAdapter(sqlCmd, conn);
          da.Fill(ds, "EmployeeInfo");
          foreach (DataRow dr in ds.Tables["EmployeeInfo"].Rows) {
            string strVal = dr.ItemArray.GetValue(0).ToString().Trim();
            if (strVal != "") cboMgr.Items.Add(strVal);
          }
          //cboMgr.DataSource = ds.Tables["EmployeeInfo"].Columns[0];
        } catch (Exception e) {
          strErr += "Error Loading the Managers:\n" + e.Message + "\n";
        }
      } catch (Exception e) {
        strErr += "Error Opening DataSet:\n" + e.Message + "\n";
      }
      if (strErr != "") {
        Console.WriteLine(strErr);
      }
    }
 
Back
Top