Using a combobox to refresh form data
I have a combobox on a form. I have a datatable that the form controls (textboxes, checkboxes) are bound to. The combobox will need to change/refresh the data based on the selection. So when a user selects another item in the combobox, the appropriate row data from the datatable is displayed.
I have a private method called databind() that is called in form_load that binds the data to the controls on the form. I assume that when the cbo_SelectedIndexChanged event first, i want to walk through my datatable and find the right row, then rebind. I am not sure how to pull that off.
Thanks in advance for any help.
Ebin RoyPosted Jul 20, 2007, 10:47 AM
Load the combobox with a class/Object
//create a class like below
Class Employee
{
private string sName;
private string sID;
Public string Name()
{
set
{
sName = value;
}
get
{
return sName ;
}
}
Public string EMpID()
{
set
{
sID = value;
}
get
{
return sID;
}
}
}
// In ur form follow the code , sorry presently I ve not tested my code, but the logic is given below
//Load Combo with List of Employee
//get from database dr is the data reader
List lstEmp = new List();
//for loop
Employee obj = new Employee();
obj.Name = dr("EmpName");
obj.EMpID = dr("ID");
lstEmp.add(obj);
//End of for loop
combo1.Datasource = lstEmp;
combo1.DisplayMember = "Name";
combo1.DisplayMember = "EMpID";
//in selectedindex changed get the selectedItem
Employee obj = (Employee) Combo1.selectedItem() //will reurn the Employee object
//You can display like this
textbox1.text = obj.Name;
textbox2.text = obj.EMpID;
Thanks.. it may help u.