S
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!
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!