Problem with OOP and Datagrid

Problem with OOP and Datagrid

Old forum URL: forums.lhotka.net/forums/t/2598.aspx


yiannisezn posted on Wednesday, March 28, 2007

I have been working lately in a 3-tier model which I find very helpful. The only problem now is that I want to selectively show some of the propperties on the datagrid.

i.e. Employee.Name, Employee.Department.Name and Employee.SalaryRange

 

Following is a helpful example. 

 

My case is:

- I have two different objects Employee and SalaryType

- The collection I use inherits from CollectionBase and includes Employee.

 

The code I use is:

Classes & Collection

 

public class EmployeeCollection:CollectionBase

{

      public SalesOutDetailsCollection()

      {}

 

      public int Add(Employee item)

      {

            List.Add(item);

            return List.Count - 1;

      }

}

 

public class Employee

{

      private int _id;

      private string _code;

      private string _name;

      private SalaryType _salary;

     

      public int Id

      {

            get { return _id; }

            set { _id = value; }

      }

 

      public string Code

      {

            get { return _code; }

            set { _code = value; }

      }

 

      public string Name

      {

            get { return _name; }

            set { _name = value; }

      }

 

      public SalayType Salary

      {

            get { return _salary; }

            set { _salary = value; }

      }

}

 

public class SalayType

{

      private int _id;

      private string _range;

     

      public int Id

      {

            get { return _id; }

            set { _id = value; }

      }

 

      public string Range

      {

            get { return _range; }

            set { _ salary = value; }

      }

}

 

public static EmployeeCollection GetList()

{

      EmployeeCollection list = new EmployeeCollection();

      SqlCeDataReader reader = DAL. EmployeeDB.GetList();

      try

      {

            while (reader.Read())

            {

                  Employee emp = new Employee();

                  emp.Id = Convert.ToInt32(reader("EmpId"));

                  emp.Code = Convert.ToString(reader("EmpCode"));

                  emp.Name = Convert.ToString(reader("EmpName"));

                  emp.SalaryType.Id =
                        Convert.ToInt32(reader("SalaryId"));

                  emp.SalaryType.Salary =
                        Convert.ToString(reader("SalaryRange"));

 

                  list.Add(new Employee(reader));

           }

      }

      catch (Exception ex)

      {

            string msg = ex.Message;

            list = null;

      }

      return list;

}

 

How can I bind this on a Datagrid and show only Employee.Name and Employee.Salary.Range for columns?

ajj3085 replied on Wednesday, March 28, 2007

I would hide the columns that display the information you don't want displayed.  Do you need to hide the data in some cases, or in all cases?

yiannisezn replied on Friday, March 30, 2007

ajj3085

 

The problem is not with hiding some columns. The problem is to show the Salary Range of the employee

 

ajj3085 replied on Friday, March 30, 2007

Write a class that wraps employee and flattens your object model.

Copyright (c) Marimer LLC