Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to show two column values on one Combo box.
like this

1. Book of Salah

1 is book number(Column Name)
Book of Salah is bookName(Column Name)

using windows forms
Posted
Updated 7-Oct-13 3:33am
v2
Comments
BulletVictim 7-Oct-13 9:53am    
Try taking a look here
http://stackoverflow.com/questions/17788368/combobox-item-filled-from-2-columns-insert-into-sqlcommand-only-one-column-sel

or here

http://stackoverflow.com/questions/17538323/how-to-display-two-different-columns-in-one-combobox

Get the data from backend database in a format you want and display the same in your combo box using the DataTextField.

Below code should help you:

Quote:
<asp:dropdownlist id="DropDownList1" runat="server" xmlns:asp="#unknown">
DataSourceID="SqlDataSource1" DataTextField="Description"
DataValueField="Sequence">

<asp:sqldatasource id="SqlDataSource1" runat="server" xmlns:asp="#unknown">
ConnectionString="<%$ ConnectionStrings:LogDBConnectionString %>"
SelectCommand="SELECT Sequence, CONVERT(varchar(50), Sequence) + ' - ' + CONVERT(varchar(50), [MessageType]) 'Description' FROM [Event]">
 
Share this answer
 
Comments
berrymaria 7-Oct-13 21:32pm    
This is not C# in Windows Forms.
ShridharPanigrahi 8-Oct-13 1:40am    
Then what it is? AJAX Combo box?
BDW the code above is not for windows forms but for Web forms FYI.
berrymaria 8-Oct-13 2:16am    
Yes, that's what I meant. See the tags above. It's C# Windows Forms. Not Web Forms.
ShridharPanigrahi 8-Oct-13 2:31am    
Ok, Thanks
XML
Replace your code with

<asp:DropDownList ID="EmployeeIDTextBox" runat="server"
DataSourceID="AccessDataSource1" DataTextField="EmployeeName" DataValueField="EmployeeID" SelectedValue='<%# Bind("EmployeeID") %>'>
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/JABILMCALLEN.mdb"
SelectCommand="SELECT [EmployeeID], ([Name] + ' ' + [LastName]) as EmployeeName FROM [Employees]">
</asp:AccessDataSource>
 
Share this answer
 
Comments
berrymaria 7-Oct-13 21:32pm    
This is not C# in Windows Forms.
hello professional


you just need to concatenate your both columns as DB level and then bind them
from your Combo box.

like this

1st make a query in sql-

select (+'( '+ cast(BookNumber as varchar(20)) +' ) ' + BookName) as BookNameCode from T_BookMaster

here casting (cast(BookNumber as varchar(20))) because i think in DB's BookNumber Column may be Int type data type



then youn need to simple bind it

like this-
C#
CmbBook.DataSource = dsbook;
CmbBook.DataTextField = "BookNameCode  ";
CmbBook.DataValueField = "BookNumber";
CmbBook.DataBind();



Happy To Help!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900