Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

    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);
     }
   }

Posted

Solved!

 

Anyone care to see the results?

 

If you find this code helpful, please post a reply letting me know:

    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);
     }
   }

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...