Monday, May 19, 2014

List of Custom Objects Bind to combobox or datagridview in single method in .net

hi all,
This is my first post regarding the .net to share my experience with you all guys.
I've faced much inconvenience when
binding a combo box in net (Windows app), because we have to write more lines to complete the task.
Eg.
            Combobox1.DataSource =GetAllEmployees();
            Combobox1.DisplayMember = "EmpName";
            Combobox1.ValueMember = "EmpID";
Suppose GetAllEmployees() is a method that returns all list of employees. This is a typical binding code. In production environment systems are developed in some kind of architecture. In this example I’m using layered architecture to avoid the complexity of the application (Never develop application in single project because earlier it's easy for maintenance (OR to maintain) but day by day it will increase the complexity and then it will be nightmare to maintain.)          

How cool if we can convert above 3 lines to single line like this,

ComboBox1.EasyBind<Employee>(GetAllEmployees(), CmbSelection.Please_Select,"EmpID", "EmpName");
OR
ComboBox1.EasyBind<Employee>(GetAllEmployees(), "EmpID", "EmpName");


We can create user defined extension methods in .net to use like predefined methods like tostring() etc ,

EasyBind is .net customized extension method which is used to customize the binding.  if we have a list of employees, it will be bound to the combo box. CmbSelection is enum to attach the default selection (Please_Select).
Appreciate any comments or issues regarding this post.
Happy coding.

                                               

1 comment: