Dynamically changing DataTextField of ListBox control

  • Thread starter Jesper Lund Stocholm
  • Start date
J

Jesper Lund Stocholm

I have a ListBox control that I bind some data to. The data is an array of
objects from a webservice. I have no way of changing the objects nor
changing the SQL "under neath" the WCF-interface I extract the objects
from.

I assign the object list to the .DataSource-property of the control and
assign the DataValue-property as well as the DataTextField-property.

But - and this I cannot seem to get my head around - I would like to
combine two fields on my object to the DataTextField-property.

It's the classic: "Display firstname as well as lastname in the ListBox".

I would think that I could accomplish this in the DataBind-event of the
control, but I cannot seem to figure out how to get the code right.

The idea was to do something like:

public void PeopleListBox_DataBind(object sender, EventArgs e)
{
// some pseudo-code:
Item.DataValueField = myObject.SocSecNumber;
Item.DataTextField = myobject.Firstname + " " + myObject.Lastname;
}

Does it make any sense?
 
M

miher

Jesper Lund Stocholm said:
I have a ListBox control that I bind some data to. The data is an array of
objects from a webservice. I have no way of changing the objects nor
changing the SQL "under neath" the WCF-interface I extract the objects
from.

I assign the object list to the .DataSource-property of the control and
assign the DataValue-property as well as the DataTextField-property.

But - and this I cannot seem to get my head around - I would like to
combine two fields on my object to the DataTextField-property.

It's the classic: "Display firstname as well as lastname in the ListBox".

I would think that I could accomplish this in the DataBind-event of the
control, but I cannot seem to figure out how to get the code right.

The idea was to do something like:

public void PeopleListBox_DataBind(object sender, EventArgs e)
{
// some pseudo-code:
Item.DataValueField = myObject.SocSecNumber;
Item.DataTextField = myobject.Firstname + " " + myObject.Lastname;
}

Does it make any sense?

Hi,

One simple way to do this would be to create an adapter class that has the
properties required for the presentation. You can do this with minimal code
like this :

IEnumerable<ModelPerson> persons = GetPersonsFromWCFService();
// now create the adapters.
var presentaionPersons= persons.Select(person => new
{
FullName = string.Format("{0} {1}", person.FirstName, person.LastName)
// ... whatever else needed for presenting a person
}).ToList();

listBox.DataSource = presentaionPersons;
listBox.DisplayMember = "FullName";
//...

Hope You find this useful.
-Zsolt
 
J

Jesper Lund Stocholm

One simple way to do this would be to create an adapter class that has
the properties required for the presentation. You can do this with
minimal code like this :

IEnumerable<ModelPerson> persons = GetPersonsFromWCFService();
// now create the adapters.
var presentaionPersons= persons.Select(person => new
{
FullName = string.Format("{0} {1}", person.FirstName,
person.LastName)
// ... whatever else needed for presenting a person
}).ToList();

listBox.DataSource = presentaionPersons;
listBox.DisplayMember = "FullName";
//...

Hope You find this useful.

I do - thank you so much :)
 
M

Murat HAKSAL

Other approaches is use interface abstract
such as
public interface IDisplayContent
{
string DisplayContent{get;}
}
impl:
class ModelPerson:IDisplayContent
{
...//some ur codes
public string DisplayContent
{
get{return this.ToString();}
}

public override ToString()
{
return string.Format("{0} {1}", person.FirstName ??string.Empty,
person.LastName??string.Empty);
}
void LoadData()
{
listbox.DataSource=GetPersonsFromWCFService();
listBox.DisplayMember = "FullName";

}

}
if u wanna apply to valuecontent or valueID. ValueContent return this,
valueID return ur ID

Murat HAKSAL
MH
 
M

Murat HAKSAL

Other approaches is use interface abstract
such as
public interface IDisplayContent
{
string DisplayContent{get;}
}
impl:
class ModelPerson:IDisplayContent
{
...//some ur codes
public string DisplayContent
{
get{return this.ToString();}
}

public override ToString()
{
return string.Format("{0} {1}", person.FirstName ??string.Empty,
person.LastName??string.Empty);
}
void LoadData()
{
listbox.DataSource=GetPersonsFromWCFService();
listBox.DisplayMember = "FullName";

}

}
if u wanna apply to valuecontent or valueID. ValueContent return this,
valueID return ur ID

Murat HAKSAL
MH

----- Original Message -----
From: "Jesper Lund Stocholm" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Monday, March 23, 2009 12:00 PM
Subject: Re: Dynamically changing DataTextField of ListBox control
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top