473,324 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

ListBox - DataSource - SelectedValue problems

Hi

NET1.1 / Winforms

I have a listbox that I am binding to a table via the DataSource property.
However I want to be able to programmatically select values in this listbox
so I am using the code below...

With lstJobTypes
.DataSource = BusinessRules.Common.JobTypes.All(True)
.DisplayMember = "Description"
.ValueMember = "JobTypeID"
End With

If Not _DefaultJobTypes Is Nothing Then
For Each j As PlasmaCommon.JobType In m_DefaultJobTypes
lstJobTypes.SelectedValue = CInt(j)
Next
End If

If I step through using the debugger, I can see the
lstJobTypes.SelectedItems.Count property increasing everytime I set the
SelectedValue and all seems good.

However when the form is then displayed the Count property becomes 1 and
only the first item in the listbox (standard functionality) is selected??!?!?!

The listbox selection mode is set to MultiExtended.

This also happens if I use the SetSelected method rather than the
SelectedValue method.

Any help is greatly appreciated as I am pulling my hair out here :(

Jul 17 '07 #1
2 5491
On Tue, 17 Jul 2007 14:48:03 +0200, Stephen Ritchie <St************@discussions.microsoft.comwrote:
Hi

NET1.1 / Winforms

I have a listbox that I am binding to a table via the DataSource property.
However I want to be able to programmatically select values in this listbox
so I am using the code below...

With lstJobTypes
.DataSource = BusinessRules.Common.JobTypes.All(True)
.DisplayMember = "Description"
.ValueMember = "JobTypeID"
End With

If Not _DefaultJobTypes Is Nothing Then
For Each j As PlasmaCommon.JobType In m_DefaultJobTypes
lstJobTypes.SelectedValue = CInt(j)
Next
End If

If I step through using the debugger, I can see the
lstJobTypes.SelectedItems.Count property increasing everytime I set the
SelectedValue and all seems good.

However when the form is then displayed the Count property becomes 1 and
only the first item in the listbox (standard functionality) is selected??!?!?!

The listbox selection mode is set to MultiExtended.

This also happens if I use the SetSelected method rather than the
SelectedValue method.

Any help is greatly appreciated as I am pulling my hair out here :(

Hi Stephen,

I did a test and could not immediatly reproduce your problem (C# code added below), but then I noticed "... when the form is displayed". Are you by chance running this code in the constructor? DataBindings can be tricky and some do not perform well at all if you manipulate them before the controls have been created.

If you are running the code in the constructor, try moving it to the load event instead. (I did get the same problem if I ran the code from the constructor, but it worked fine from the load event or using a button totrigger it).

protected override void OnLoad(EventArgs e)
{
base.OnLoad (e);
button1_Click(this, null);
}

int[] def = new int[]{1, 3, 5};
private void button1_Click(object sender, System.EventArgs e)
{
ArrayList list =new ArrayList();
list.Add(new MyObj("Zero", 0));
list.Add(new MyObj("One", 1));
list.Add(new MyObj("Two", 2));
list.Add(new MyObj("Three", 3));
list.Add(new MyObj("Four", 4));
list.Add(new MyObj("Five", 5));
list.Add(new MyObj("Six", 6));
listBox1.DataSource = list;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "ID";

listBox1.SetSelected(0, false); // to prevent the default item selected
foreach(int i in def)
{
listBox1.SelectedValue = i;
}
}

class MyObj
{
private string _Name;
private int _ID;
public string Name
{
get{return _Name;}
set{_Name = value;}
}
public int ID
{
get{return _ID;}
set{_ID = value;}
}
public MyObj(string name, int id)
{
this.Name = name;
this.ID = id;
}
}
--
Happy coding!
Morten Wennevik [C# MVP]
Jul 18 '07 #2
Hi Morten

The code does already run within the form load event. If I move the code out
to a standard windows form it works okay, whereas within my application the
listbox (which is the standard windows control) is contained within a user
control, which is contained in another user control that sits on a
derived/inherited form, so I think this layering is having an effect :(

Subsequently the customer has now decided they want it to be a checked
listview, and this works perfectly well :)

This problem has been filed under "just one of them things" :)

"Morten Wennevik [C# MVP]" wrote:
On Tue, 17 Jul 2007 14:48:03 +0200, Stephen Ritchie <St************@discussions.microsoft.comwrote:
Hi

NET1.1 / Winforms

I have a listbox that I am binding to a table via the DataSource property.
However I want to be able to programmatically select values in this listbox
so I am using the code below...

With lstJobTypes
.DataSource = BusinessRules.Common.JobTypes.All(True)
.DisplayMember = "Description"
.ValueMember = "JobTypeID"
End With

If Not _DefaultJobTypes Is Nothing Then
For Each j As PlasmaCommon.JobType In m_DefaultJobTypes
lstJobTypes.SelectedValue = CInt(j)
Next
End If

If I step through using the debugger, I can see the
lstJobTypes.SelectedItems.Count property increasing everytime I set the
SelectedValue and all seems good.

However when the form is then displayed the Count property becomes 1 and
only the first item in the listbox (standard functionality) is selected??!?!?!

The listbox selection mode is set to MultiExtended.

This also happens if I use the SetSelected method rather than the
SelectedValue method.

Any help is greatly appreciated as I am pulling my hair out here :(

Hi Stephen,

I did a test and could not immediatly reproduce your problem (C# code added below), but then I noticed "... when the form is displayed". Are you by chance running this code in the constructor? DataBindings can be tricky and some do not perform well at all if you manipulate them before the controls have been created.

If you are running the code in the constructor, try moving it to the load event instead. (I did get the same problem if I ran the code from the constructor, but it worked fine from the load event or using a button to trigger it).

protected override void OnLoad(EventArgs e)
{
base.OnLoad (e);
button1_Click(this, null);
}

int[] def = new int[]{1, 3, 5};
private void button1_Click(object sender, System.EventArgs e)
{
ArrayList list =new ArrayList();
list.Add(new MyObj("Zero", 0));
list.Add(new MyObj("One", 1));
list.Add(new MyObj("Two", 2));
list.Add(new MyObj("Three", 3));
list.Add(new MyObj("Four", 4));
list.Add(new MyObj("Five", 5));
list.Add(new MyObj("Six", 6));
listBox1.DataSource = list;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "ID";

listBox1.SetSelected(0, false); // to prevent the default item selected
foreach(int i in def)
{
listBox1.SelectedValue = i;
}
}

class MyObj
{
private string _Name;
private int _ID;
public string Name
{
get{return _Name;}
set{_Name = value;}
}
public int ID
{
get{return _ID;}
set{_ID = value;}
}
public MyObj(string name, int id)
{
this.Name = name;
this.ID = id;
}
}
--
Happy coding!
Morten Wennevik [C# MVP]
Jul 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Irwin M. Fletcher | last post by:
ListBox DataSource DisplayMember Property Problems in C# I have a (single select) listbox with data and when I click on the list I can't get the right text selected. My listbox is setup...
0
by: Jesse Martinez | last post by:
My problem occurs when I use an ArrayList as a ListBox.DataSource. When the ArrayList attached to the Listbox is not empty the Listbox behave normal without problem, but if I remove all items...
4
by: kids_pro | last post by:
Hi, On form_load() I bind a combobox to a dataset and listbox to a datatable. When I want to unbind it I call comboBox1.DataSource = null, listBox1.DataSource = null It unbind but the Items...
1
by: Zoury | last post by:
Hi there! :O) I have a WebService that contains the following struct definition. this struct is defined right after the WebService class, ie : //*** namespace mynamespace { public class...
4
by: dtblankenship | last post by:
Hello everyone, I know this question has been asked many times in the forums, and after spending a few days reading, I am still confused as to the answer. I have a ListBox (lstBox),...
1
by: COHENMARVIN | last post by:
I am trying to display the contents of a directory in a listbox. I manage to get an array of fileInfo objects, and when I print them out they indeed contain several file names. But when I do...
2
by: Zorpiedoman | last post by:
Ok, I have a DataTable: ID Name -- ------------ 1 John 2 Jacob 3 Jingleheimer 4 Schmidt 5 John
1
by: clickon | last post by:
Forget about the controlParameter for the moment, for testing purposes i have created the following Markup: <asp:Table ID="tblSelectRoute" runat="server" CssClass="asp-table"> <asp:TableRow>...
1
by: WhiteWizard | last post by:
First my apologies, this may be longer than the normal question. I have a windows app (.NET 2.0, VS2005), and I've written a user control that will allow the user to "drag and drop" a directory...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.