0
votes

My EmployeeDB class

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data;

namespace Test { public class EmployeeDB { private string connectionString;

    public EmployeeDB()
    {
        //my connectionstring info
    }

    public int CountEmployees()
    {
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("CountEmployees", con);
        cmd.CommandType = CommandType.StoredProcedure;

        try
        {
            con.Open();
            return (int)cmd.ExecuteScalar();
        }
        catch (SqlException err)
        {
            // Replace the error with something less specific.
            // You could also log the error now.
            throw new ApplicationException("Data error.");
        }
        finally
        {
            con.Close();
        }
    }

    public List<EmployeeDetails> GetEmployees()
    {
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("GetAllEmployees", con);
        cmd.CommandType = CommandType.StoredProcedure;

        // Create a collection for all the employee records.
        List<EmployeeDetails> employees = new List<EmployeeDetails>();

        try
        {
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                EmployeeDetails emp = new EmployeeDetails(
                    (int)reader["EmployeeID"], (string)reader["FirstName"],
                    (string)reader["LastName"], (string)reader["TitleOfCourtesy"]);
                employees.Add(emp);
            }
            reader.Close();

            return employees;
        }
        catch (SqlException err)
        {
            // Replace the error with something less specific.
            // You could also log the error now.
            throw new ApplicationException("Data error.");
        }
        finally
        {
            con.Close();
        }
    }
}

}

My employeeDetails class

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Test
{
    public class EmployeeDetails
    {
private int employeeID;
        private string firstName;
        private string lastName;
        private string titleOfCourtesy;

        public int EmployeeID
        {
            get {return employeeID;}
            set {employeeID = value;}
        }
        public string FirstName
        {
            get {return firstName;}
            set {firstName = value;}
        }
        public string LastName
        {
            get {return lastName;}
            set {lastName = value;}
        }
        public string TitleOfCourtesy
        {
            get {return titleOfCourtesy;}
            set {titleOfCourtesy = value;}
        }

        public EmployeeDetails(int employeeID, string firstName, string lastName,
            string titleOfCourtesy)
        {
            this.employeeID = employeeID;
            this.firstName = firstName;
            this.lastName = lastName;
            this.titleOfCourtesy = titleOfCourtesy;
        }

        public EmployeeDetails(){}

    }
}

Then i build the class library and add the reference to my windows form project.

Here is a screenshot of my main form that the class shows up however there are no methods. enter image description here

1

1 Answers

1
votes

To bind to a DataGrid:

  1. Add a BindingSource (bindingSource) to the form
  2. Set the DataSource property of the DataGrid to bindingSource
  3. Set the DataSource property of the bindingSource to the result of your GetEmployees()

If you have the following implementation for your library like:

public interface IDataOperation
{
    List<Employee> GetEmployees();
}

public class DataOperation : IDataOperation
{
    public List<Employee> GetEmployees(){}
}

Your implementation should look like:

IDataOperation dataOperation = new DataOperation();

var bindingSource = new BindingSource();
dataGrid.DataSource = bindingSource;
bindingSource.DataSource = dataOperation.GetEmployees();

Or, you could just simply set the DataSource property of your DataGrid to the result of GetEmployees() programatically without using BindingSource:

dataGrid.DataSource = dataOperation.GetEmployees();

EDIT: In your screenshot, you should instantiate your EmployeeDB before using it like the code below:

Test.EmployeeDB employeeDB = new Test.EmployeeDB();
dataGrid.DataSource = employeeDB.GetEmployees(); // assuming you have a dataGrid control