Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating a ComboBox

Status
Not open for further replies.
Mar 9, 2006
93
CA
I have a combo box that I am populating. I fill the comboBox alrite but the string that diplays in the combobox is not what I want. Here is my code:
for(int i =0; i < dt.Rows.Count; i++)
{
cmb.Items.Add(new recordClass(dt.Rows["RecordType"].ToString(), Convert.ToInt32(dt.Rows["RecordId"])));
}

Inside the combobox is reading: trackingsytem.RecordClass
- I want the contents of dt.Rows["RecordType"] to appear in the combobox. Any help would be appreciated.
Matt
 
dt.Rows["RecordType"]

Then why don't you put this:

Code:
for(int i =0; i < dt.Rows.Count; i++)
{
cmb.Items.Add(dt.Rows[i]["RecordType"].ToString());                    
}

or


Code:
        cmb.DataTextField = "RecordType";
        cmb.DataValueField = "RecordID";
        cmb.DataSource = dt;
        cmb.DataBind();
 
Code:
[b]cmb.Items.Add(new recordClass(dt.Rows[i]["RecordType"].ToString(), ...[/b]
You're adding the recordClass object itself. The default method, recordClass.ToString(), returns the class name, hence the "error" you have when the item is finally rendered.

On your implementation of recordClass type, override ToString() to return the required field instead
Code:
public override string ToString() {
    return someVar;
}
Or call the appropriate recordClass field/property that you want to add on the combobox.
 
your recordClass exposes a public property that returns the value for dt.Rows["RecordType"]...right?

right below in the call to "InitializeComponent()", you need to insert the following:

cmb.DisplayMember = "" <---- public Property name goes here.



This will solve your problem.

Scott
Programmer Analyst
<{{><
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top