Click here to Skip to main content
16,004,974 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am wondering if there is a method to databind the "Selected" property in a ListBox similar to the way that you can Databind the DisplayMember or ValueMember.

I have a List<myobject> where MyObject contains an ID (valuemember), Name (displaymember) and Active boolean flag (selected?) and I would like an easy way to map it so that the Listbox will understand when the Active flag is changed from one value to the other.

I have looked around the web and yet to find something so I somewhat doubt it is possible but just in case... or if anyone has alternate solutions it would be greatly appreciated.

== EDIT ===

The object I am trying to databind is not a database table but a List of objects (although I presume the idea is basically the same). The object is very simple like the following (I am using .NET 2 so I don't have default get and set):

<br />
public class MyObject<br />
{<br />
  private int id;<br />
  public int ID { get { return id; } set { id = value; } }<br />
<br />
  private string name;<br />
  public string Name { get { return name; } set { name = value; } }<br />
<br />
  private bool flag;<br />
  public bool Flag { get { return flag; } set { flag= value; } }<br />
}<br />
<br />
List<MyObject> myObjectList = new List<MyObject>();<br />
<br />


Sunasara, your assumption is correct: I want the ListBox to show the item as selected if Flag == true and unselected if Flag == false.
Posted
Updated 14-Dec-09 8:20am
v3

Hi,
Maikeru2000

I got your problem and there is no methods to bind ListBox with multiselect options; i assumed you have a datatable which contains three column like Id,Name or Flag. You have to bind somthing like?

my assumption is:
if Flag contain true it will select an item else unselect.


please let me know your comments, if my assumption is correct, so i'll give you a solution :)

Thanks & Regards
Imdadhusen
 
Share this answer
 
<br />
//This is the class<br />
public class MyObject<br />
{<br />
    private int id;<br />
    public int ID { get { return id; } set { id = value; } }<br />
<br />
    private string name;<br />
    public string Name { get { return name; } set { name = value; } }<br />
<br />
    private bool flag;<br />
    public bool Flag { get { return flag; } set { flag = value; } }<br />
<br />
    public MyObject(int id, string name, bool flag)<br />
    {<br />
        ID = id;<br />
        Name = name;<br />
        Flag = flag;<br />
    }<br />
}<br />
<br />
//Using above class<br />
public partial class Demo : System.Web.UI.Page<br />
{<br />
    protected void Page_Load(object sender, EventArgs e)<br />
    {<br />
        List<MyObject> myObjectList = new List<MyObject>();<br />
        myObjectList.Add(new MyObject(1, "Imdadhusen", true));<br />
        myObjectList.Add(new MyObject(2, "Sunasara", false));<br />
        myObjectList.Add(new MyObject(1, "ASP.Net", true));<br />
        myObjectList.Add(new MyObject(1, "C#", true));<br />
        myObjectList.Add(new MyObject(1, "Java", false));<br />
        myObjectList.Add(new MyObject(1, "JSP", false));<br />
        myObjectList.Add(new MyObject(1, "JavaScript", true));<br />
<br />
        bindListBox(myObjectList, ListBox1);<br />
    }<br />
<br />
    //Bind Listbox with object<br />
    //Note: please make sure your listbox SelectionMode property should be Multiple<br />
    public void bindListBox(List<MyObject> objList, ListBox lst)<br />
    {<br />
        if (objList != null)<br />
        {<br />
            for (int i = 0; i < objList.Count; i++)<br />
            {<br />
                ListBox1.Items.Add(new ListItem(objList[i].Name, objList[i].ID.ToString()));<br />
                if (lst.SelectionMode != ListSelectionMode.Single) ListBox1.Items[i].Selected = objList[i].Flag;<br />
            }<br />
        }<br />
    }<br />
<br />
}<br />
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900