473,652 Members | 3,162 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(stri ng 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.ComboIte ms(str, id));
}
ctrl.DataSource = Addr;
ctrl.DisplayMem ber = "DisplayedText" ;
ctrl.ValueMembe r = "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\nPara meter name: newDisplayMembe r"

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.InvalidC astException was unhandled
Message="Specif ied cast is not valid."

In the SelectedIndexCh anged Code the key part is:

int SelectedValue = (int)lstKPI.Sel ectedValue;
if (PopulateKPIDet ails(SelectedVa lue) == true)
{ etc ...

If I look at the contents of the "lstKPI.Selecte dValue" 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 2067

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

Similar topics

6
7476
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 "System.Data.DataR..." is displayed inside the combobox is the amount of records in the dataset. On the other hand, if my query is "select emplcode from payemployee", the databind will work fine (but I don't want to limit the dataset to one field)....
1
2622
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 Products table.
4
7816
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 this results in the BindingContext's Current.Row.Rowstate becoming 'Modified' whenever the BindingContext position changes. What is the RIGHT way to go about this? Thanks, -- Pat
30
4566
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 corresponding field in the lookup table. In my data table we store the ID in what I will call the 'key' field. == Description of the desired operation:
5
9155
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 comboQueryTitle_SelectedIndexChanged handler to, when a new selection is made, update the query and refresh the display. The only problem is that the program seems to inexplicably start with the ..selectedindex property at -1! This makes no sense to me. ...
6
2873
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 and the coresponding 'Id' from the lookup table is to be inserted into the field of the new record. I have two simple tables. "tblPerson" is the data table. The lookup
2
8662
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 list of values from a table, and as the user selects values, a datagrid displays related records from another table because it is bound via FK relationship. My table: /****** Object: Table . Script Date: 06/19/2006
19
2196
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 like Text replaces an item or something like that. This results in a runtime error at which time I learn that the item that should be CQQ is a String.
3
7505
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 on. It's a simple program that loads a control onto a form and binds "Foo" against a combobox ("SelectedItem") for it's "Bar" property and a datetimepicker ("Value") for it's "DateTime" property. The DateTimePicker.Visible value is set to...
0
8367
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8279
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8703
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7302
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5619
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.