-->

Saturday, June 1, 2013

Change textbox value according to Combo Box in c# programming: WinForms

In our previous post we have learnt perform binding Combo Box and Listbox, now it’s time to get access their values and use them to bind another controls in winforms. I am binding a combo box with an employee list and them access data in textbox in c# programming.


For example we have a c# class Employee having following properties:

public class Employee
{
        public int No { get; set; }
        public string Name { get; set; }
}

Create an object of type List of employee (in our Form1.cs file) that will be used to in binding of combo Box

List<Employee> empList = new List<Employee>();

To bind with combo Box this list should have some set of items, so we will add some employees with use of following code in our Form1's constructor:

empList.Add(new Employee() { No = 111, Name = "Venkatesh" });
empList.Add(new Employee() { No = 222, Name = "Muthu Kumaran" });
empList.Add(new Employee() { No = 333, Name = "Murli Prasad" });
empList.Add(new Employee() { No = 444, Name = "Ghosh Babu" });
empList.Add(new Employee() { No = 555, Name = "Jacob Martin" });

Now using following two lines of code we will bind our comboBox with this above list of item

comboBox1.DataSource = empList;
comboBox1.DisplayMember = "No";

When Form1 will run, combo Box will have above list of items and textbox will be empty. To display related value of employee name we have to write these two lines of code with combo Box binding:

Employee selectedEmployee = comboBox1.SelectedItem as Employee;
textBox1.Text = selectedEmployee.Name;

Now our article’s main aim is to binding a textbox with combo Box selected value, so there is an Event of combo Box i.e. comboBox_SelectionChanged event. So I will repeat the most recent two lines of code in this event as written below:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
     Employee selectedEmployee = comboBox1.SelectedItem as Employee;
     textBox1.Text = selectedEmployee.Name;
}

Check your windows form application, when you change the selected item of combo Box it will also change the related name of Employee in textbox’s text property.

Download Example

Monday, May 27, 2013

Combo Box or List Box Binding with Database, Entity Framework 5

Most often when we have a list of items and want to select a single item at a time then we can use Combo Box or List Box in winforms application. The basic difference in both is: In List Box one can select an item only from existing list of items whether in Combo Box one can write his/her own new data that can be added to our binding list.

There are some properties that are used to bind a list of items in c# programming i.e

  • Data Source
  • Display Member
  • Value Member
DataSource property will decide the list of data that is to be bind. It may be a Database, Web Service and an object. When this property is set, the item collection cannot be modified at run time.

DisplayMember property indicates the name of an object contained in DataSource property. It denotes what object will be displayed to the user. The default value is empty string.

ValueMember property is used to get the value associated with the control when we are accessing the selected item property.

Let's suppose we have an employee class with properties as shown below c# class:

public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Department { get; set; }
    }

Now following code is used to get all records of employee from Database into a var type of variable named employees as given below:

DataContext dc = new DataContext();
var employees = from emp in dc.Employees
                          select emp;

In the above code employees variable have IEnumerable<Employee> type of data and this type of data cannot directly bind to data source property of a control in our programming language. So we have to convert it to a List<Employee>.
To convert this simple type of data a ToList() method is sufficient.

In case of Combo Box

ComboBox1.DataSource = employees.ToList();
ComboBox1.DisplayMember = "Name";
ComboBox1.ValueMember = "Id";

In case of List Box

ListBox1.DataSource = employees.ToList();
ListBox1.DisplayMember = "Name";
ListBox1.ValueMember = "Id";

Here ToList() method is used to convert IEnumerable<Employee> type of data to List<Employee>.
Now when we run our project we will see the list of employees Names in the item collection of Combo Box or ListBox.
© Copyright 2013 Computer Programming | All Right Reserved