Binding ComboBox control

DimkaNewtown

Freshman
Joined
Jan 21, 2003
Messages
43
Location
New York City
I have a combobox control that is bound to a dataset view. The code works perfectly. (The recordset is an ADO 2.6 Recordset)

Code:
Recordset rs = new Recordset();
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter();

Global.CS.GetAvailableEquipByType( Global.ConnectString, ref rs, ref _EquipmentType );

da.Fill(ds, rs, "EquipmentByType");

cmbEquipment.DataSource = new DataView( ds.Tables[0] );
cmbEquipment.ValueMember = "s_GUID";
cmbEquipment.DisplayMember = "SN";

My problem is that I want to display a combination of fields (which is determined client-side programmatically) in a format such as "Serial Number - Expiration Date - Model Name".

Now, is there a way to bind the DisplayMember property in such a way? I really don't want to iterate through the entire DataSet manually.
 
You could use an expression column. Add a new column to your dataset's table and set it's Expression property.

For example (untested, but should be close):
Code:
...
da.Fill(ds, rs, "EquipmentByType");

ds.Tables[0].Columns.Add("CustData", typeof(string), "SN + ' - ' + ExpDate + ' - ' + ModelName");

cmbEquipment.DataSource = new DataView( ds.Tables[0] );
cmbEquipment.ValueMember = "s_GUID";
cmbEquipment.DisplayMember = "CustData";

You can check out expression columns in the help for more info on what you can and can't put in them. For instance, if the columns "SN", "ExpDate", or "ModelName" might be null, you may have to wrap them with IsNull(SN, '') (for example) so that the whole string concatenation doesn't return null.

I also changed the DisplayMember to point to the new column, CustData.

-Nerseus
 
Back
Top