WinForms Databinding and NULL values

J

Jack Tripper

Here's the scenario:

One Form that shows Employee. Employee class is as below:


class Employee {
public int EmployeeID { get;set;}
public string Name { get;set;}
public string Address { get;set;}
public int? ReportsToEmployee { get;set;}
}

The form is bound using Object Data Source with Employee class.
The idea is that I need to bind a List<Employee> to a Form. So we do this:

List<Employee> _employees = new List<Employee>(10);
_employees.Add(new Employee(....));
....

List<Employee> _managers = new List<Employee>(_employee);

employeeBindingMangaer.DataSource = _employees;
//managerComboBox.DataSource = _managers;


Now when we open the form, all is working. However if I need to specify a
reporting to for an employee as None or NULL,
I cannot do that because the displayed values are coming from database and
there is no NULL row. So one I have set this,
I cannot clear the selected choice.

I have figured the following ways:

Hack 1: Create a NULL employee object with ID=0, add to _managers and then
on value changed event, if EmployeeID is 0, set the selection to empty.
However if we will need to manually manage these null objects.


Any other way of doing this?
Can we use IExtenderControl to handle this somehow?

Any better/cleaner way? I need to do this at a lot of places in code.

Please help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top