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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting values and text to listbox from array

Status
Not open for further replies.

dazza12345

Programmer
Nov 25, 2005
35
GB
Hi all, im using an array to add values to a listbox. The array comes from values in a database. Once the values are added to the array, the follwoing lines of code add the values in the array to the listbox.

Code:
string[] accountantArray;
        accountantArray = AgentDetailsDataFunctions.GetAllAgentDetails(userID, "accountant");
        AccountantList.DataSource = accountantArray;
        AccountantList.DataBind();
        AccountantList.Items.FindByText("Add new accountant").Selected = true;

The array at present holds the Forename and Surname of the accountant in the database. Is tehre any way of getting the name to be displayed in the listbox (i.e. the text), but have the value of the text box as something else (say the accountantID)?

Is there any way of creating an array that not only holds the name from the database but also the userID?

Cheers
 
Rather than create an array, you could create a DataTable and have the first column hold the value and the second hold the text. You can then set the relevant fields in the ListBox's DataTextField and DataValueField properties.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi, ive got this far with creating a datatable. However, i can seem to find a command that enables me to output the values in the table into the listbox.

Code:
        DataTable Agenttable = new DataTable("Agenttable");
        Agenttable.Columns.Add("Value");
        Agenttable.Columns.Add("Text");
        Agenttable.Rows.Add("1", "2");

i.e. what method do I need to use below:

Code:
listboxname.DataTextField = Agenttable.????
listboxname.DataValueField = Agenttable.????
 
They both take string arguments e.g.
Code:
listboxname.DataTextField = "Text"
listboxname.DataValueField = "Value"


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
UI understand that both take in strings, but what I want to know is what method of the data table do i need to use to get the values into the listbox.
 
Exactly the same method as you used to bind the ListBox to an Array...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
You'll need to bind the table to the grid with DataSource() and DataBind(), then set the Text and Value properties.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top