0
Answer

Generic ComboBox/ListBox loader function

r_butler_999

r_butler_999

18y
2.3k
1
Hi all

I have a function which takes a combobox and a storedProcedure name, and populates the combo from a SQLdataReader object, something like this :

public static void LoadComboData(ComboBox cboToPopulate,string procName,Paras p,SqlConnection sqlConn,string displayField,string itemField,LoadDataForComponent.DataType dataType,int startIndex)

{

SqlDataReader sqlData = StoredProcedure.GetDataReader(procName,p,sqlConn);

cboToPopulate.BeginUpdate();

cboToPopulate.Items.Clear();

cboToPopulate.DisplayMember = DISP_MEMBER;

cboToPopulate.ValueMember = VALUE_MEMBER;

int looper;

AddItemsToIList (cboToPopulate.Items,sqlData,displayField,itemField,dataType,out looper);

cboToPopulate.EndUpdate ();

if ((startIndex>-1) && (looper>0))

{cboToPopulate.SelectedIndex=0;}

sqlData.Close();

}


Recently, I needed a function to populate a ListBox in the same way. Now I noticed that the Combo and the Listbox have the same method calls (BeginUpdate, .Items.Clear, DisplayMember, ValueMember, EndUpdate), so I thought there must be a way of just using one function and having a generic object to handle them both. However, although I think this should be possible, I don't know how to do it. What I have done, is rip out the actualt populating routine so I pass an IList object from both functions). But can someone help in writing a single function that handles both objects? Or is it not possible? Thanks in advance

Rog