473,508 Members | 2,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Combobox binding error - Help needed.

Siv
Hi,
I have a class that I want to use generically to add items to a combobox so
that I have a display item and an ID item that isn't displayed but that I
can use to display the relevant database record for when the user selects an
entry in the combo.

This is my generic class:

public class ComboItems
{

private string ListText = ""; //What appears in the text part of
the combo
private int ReturnVal = 0; //What is returned by the combo
usually an ID value

public ComboItems(string strListText, int intReturnValue)
{
ListText = strListText;
ReturnVal = intReturnValue;
}
public string DisplayedText()
{
return ListText;
}

public int ReturnedID()
{
return ReturnVal;
}

public override string ToString()
{
return ListText + " - " + ReturnVal;
}
}

In my code that adds items to the combo I have the following:

ArrayList Addr=new ArrayList();
string str ="";
int id = 0;

while (RsK.Read())
{
str = string.Format("{0,-30} Band
{1:00}",RsK["KPI_Name"], RsK["Band"]);
id = Convert.ToInt32(RsK["KPI_ID"]);
Addr.Add(new Common.ComboItems(str, id));
}
ctrl.DataSource = Addr;
ctrl.DisplayMember = "DisplayedText";
ctrl.ValueMember = "ReturnedID";

I have stripped out the database stuff as it checks out OK, "KPI_Name" is an
NVARCHAR of length 50 and "Band" is a TINYINT.

When I place the ctrl.DataSource = Addr; line before the Display and
Valuemember lines I get:

"Cannot bind to the new display member.\r\nParameter name: newDisplayMember"

After rewading some posts and Googling I found that perhaps I should set the
datasource to the combo after specifying the value and display members.
When I do that the error doesn't occur but when I try and obtain the ID
field "KPI_ID" which I want to be in the ValueMember field I get:

System.InvalidCastException was unhandled
Message="Specified cast is not valid."

In the SelectedIndexChanged Code the key part is:

int SelectedValue = (int)lstKPI.SelectedValue;
if (PopulateKPIDetails(SelectedValue) == true)
{ etc ...

If I look at the contents of the "lstKPI.SelectedValue" it seems to have it
as follows:

{NTU % To Sales Band 02 - 2}
ListText: "NTU % To Sales Band 02"
ReturnVal: 2
I just want the ReturnVal of 2, it is giving me what appears to be both
parts of teh Arraylist glued together. Can someone explain why I am getting
this, or why if I do it the way I want to do it I get a binding error??

Any help greatfully accepted as I am sick of banging my head off the wall
now!
--
Siv
Martley, Near Worcester, United Kingdom.
Jun 18 '06 #1
0 2058

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

Similar topics

6
7469
by: Omar | last post by:
When I try to databind my comboBox (specifically field "emplcode") to a filled dataset , the contents of the comboBox displays a bunch of "System.Data.DataRowView". I assume the amount of times...
1
2613
by: Bruce | last post by:
Hi, there, I meet a problem about comboBox binding. -------------------- Database: Northwind Tables: 1) Products 2) Categories I create a form (named "form1") to edit the record from...
4
7809
by: pmcguire | last post by:
I have 2 bound ComboBoxes. I want the datasource of the second to be limited by the selection made in the first. I can do this by responding to the SelectionIndexChanged event on the first, but...
30
4517
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
5
9149
by: Eric A. Johnson | last post by:
Hi Everyone, I am at my wit's end here. I have a combobox (combyQueryTitle) that I need to use in order to select a query for my database project. Therefore, I am using the...
6
2842
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox...
2
8646
by: shumaker | last post by:
I have a combobox that is very much like the one found in the RSS project here: http://msdn.microsoft.com/vstudio/express/visualCSharp/learning/ My projectNameComboBox basically is filled with a...
19
2186
by: active | last post by:
I'm using a ComboBox to display objects of a class I've defined, say CQQ. Works great except somehow I occasionally set an Item to a String object instead of an object of type CQQ. It looks...
3
7491
by: Simon Tamman | last post by:
I've come across an interesting bug. I have workarounds but i'd like to know the root of the problem. I've stripped it down into a short file and hope someone might have an idea about what's going...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.