DiaplayMember = 2 Fields concatenated

  • Thread starter Thread starter Tim Jarvis
  • Start date Start date
T

Tim Jarvis

Hi,

I posted this earlier in the databinding group, but that group appears
to be pretty dead.

Is it possible to bind a combo box to a table, but have a DisplayMember
that is a concatenation of 2 fields? i.e. I have FirstName and LastName
columns but want to display Firstname <space> LastName in the combobox.

All help will be appreciated.

Cheers Tim.
 
Well, it is possible. You need first to prepare a special collection, for example using ArrayList collection with objects that will be bound to the combo box.

The class with data that will be bound/displayed:

class DataSourceValue
{
public string m_strName;
public int m_iVal;

public DataSourceValue (string strName, int iVal)
{
m_strName = strName;
m_iVal = iVal;
}

public string Name
{
get { return m_strName; }
}

public int Value
{
get { return m_iVal; }
}
}

and populating the combo box:

arrValues.Add (new DataSourceValue ("value to display", 10));
arrValues.Add (new DataSourceValue ("value to display 2", 12));
cb.DataSource = arrValues;
cb.DisplayMember = "Name";
cb.ValueMember = "Value";

If you keep the data in a database, then you can populate this by preparing appropriately data retrieval. If you have a table with fields id, FirstName, LastName you can:
- use a stored procedure to retrieve data, a view with calculated column or calculated columns on the SQL Server
- use an expression column in the dataset

Then, once you have the dataset with appropriate data, just use it as a data source for the combo box.
 
Cezary Nolewajka wrote:

Then, once you have the dataset with appropriate data, just use it as
a data source for the combo box.

Thanks Cezary,

something to consider, I was hoping to avoid duplication of data
though, The dataset that I have will have been retrieved either from a
local xml file (if in offline mode) or from a .net remoting server, so
i will have quite a bit of data locally. Having said that though, the
contents of the list box shouldn't be too bad, so your approach may be
the thing to try.

Thanks again.

Tim
 
You cannot avoid data duplication with the methods I suggested. The
expression columns in ADO.Net are as well stored in memory:

Unlike what happens with computed columns in most DBMS, a computed ADO.NET
DataColumn object represents a true column in the sense that all the values
are stored in memory all the time. A computed column, though, is permanently
marked as read-only. In other words, you cannot remove the readonly
attribute for the column until the bound expression is removed. Of course,
the readonly attribute also prevents any application to assign direct values
to the cells of the column.

This is a quote from the below article. Have a look at it.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndive/html/data05312002.asp

I suppose, you could write your own data provider, that calculates the
values, when requested, but anyway, then the combo/list box would store the
values anyway I think.
 
Cezary said:
I suppose, you could write your own data provider, that calculates the
values, when requested, but anyway, then the combo/list box would
store the values anyway I think.

I wonder what would be more simple, a custom data provider or a custom
combo box....

Thanks for you help, it has been appreciated.

Cheers Tim.
 
Back
Top