Results 1 to 6 of 6

Thread: adding items to a list box using a dataset

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    adding items to a list box using a dataset

    Hello,

    I have a 1 table with a list of departments, and another table with a list of employees who work in those departments.

    What I want to do is select a department from a combo box, and display all the employees who work in that department in a list box.

    I am using a dataset and created the data relationship between the 2 tables. My code is as follows:

    Code:
    OleDbCommand cmd = cnn.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT * FROM Employee";
    da.SelectCommand = cmd;
    da.FillSchema(ds,SchemaType.Source,"Employee");
    da.Fill(ds,"Employee");
    
    cmd.CommandText = "SELECT * FROM Department";
    da.SelectCommand = cmd;
    da.FillSchema(ds,SchemaType.Source,"Department");
    da.Fill(ds,"Department");
    
    //Create the relationship for the employee and department tables
    DataColumn parentColumn = ds.Tables["Department"].Columns["DepartmentCode"];
    DataColumn childColumn = ds.Tables["Employee"].Columns["DepartmentID"];
    
    ds.Relations.Clear();
    DataRelation drEmployees = new DataRelation("EmployeeDetails",parentColumn,childColumn);
    ds.Relations.Add(drEmployees);
    
    //Use a for loop to add all the employees into a list box - have problem with this part
    foreach employee in departments
    Add to the list box
    Many thanks in advance,

    Steve
    steve

  2. #2
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: adding items to a list box using a dataset

    why not use left outer join?
    VB Code:
    1. 'in you form load
    2. Dataset ds=new dataset();
    3. ds.tables.add(new datatable("dt"));
    4. 'cn is a connection
    5. 'assuming the department code are bind to your combobox
    6. 'put this code to your combobox selected index changed
    7. oledbdataadapter da = new oledbdataadapter("select * from employee e left outer join departments d on e.departmentid='" & combobox1.text & "'",cn);
    8. da.fill(ds.tables[0]);
    9. foreach(datarow dr in ds.tables[0].rows){
    10. listbox1.items.add(dr[0].tostring());<---it fills the first column data to your listbox
    11. }

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: adding items to a list box using a dataset

    If you want the Employees in the second list to be updated when the selection in the Department list is changed you should bind your Employee control to a DataView of the Employee table. You then use the RowFilter of the DataView to display only the Employee rows that match the selected Department. To make this easier you could use one of two options.

    1. Use a join to include the Department name in each Employee row. Your RowFilter property could then use the SelectedItem of the Department ComboBox.
    2. Bind the Department ComboBox to the Department table with a DisplayMember of the Department name and a ValueMember of the Department ID. Your RowFilter property could then use the SelectedValue of the Department ComboBox.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: adding items to a list box using a dataset

    Hello,

    Thanks for your reply.

    I was hopping to do all this by using the data relations. As l am learning how to use them.

    Steve
    steve

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: adding items to a list box using a dataset

    I would think that changing a single property to update your dependent list would be preferable to using a loop any day. DataRelations have other uses but they don't really help you here.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: adding items to a list box using a dataset

    Hello,

    Thanks for all your replys. I have finished the coding and now the program works the way l want it to work.

    Please tell me if there is something that you can add to this, maybe there is a better and more efficient way to do it.

    thanks in advance,

    Steve

    Code:
    //Find the selected department to search for employees in
    			DataRow[] SelectDepartment = ds.Tables["Department"].Select("DepartmentName =  '" + cboEmployeeDepartments.Text + "'");			
    			//Obtain all the employees working in this department
    			DataRow[] EmployeesRows = SelectDepartment[0].GetChildRows("EmployeeDetails");
    			//Loop through all the employees and add them to the list book
    			for ( int i = 0; i < EmployeesRows.Length; i++ )
    			{
    				lstEmployeeInDepartments.Items.Add(EmployeesRows[i]["FirstName"]);
    			}
    steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width