Link to home
Create AccountLog in
Avatar of JCWEBHOST
JCWEBHOST

asked on

dropdownlist text field format

hey guys, i have a dropdownlistbox and i am pulling information from a database,

i want to display information like this in the list

name(surname)

here is my c# code which display only the name and the ID number has a value

        ddIndentityNumber.Items.Clear();
        ddIndentityNumber.Items.Add("List of drivers names:");
        ddIndentityNumber.DataSource = D.Return_driver();
        ddIndentityNumber.DataTextField = "name";
        ddIndentityNumber.DataValueField = "indentity_number";
        ddIndentityNumber.DataBind();

please some one show me how to join the name with the surname?
Avatar of Imran Javed Zia
Imran Javed Zia
Flag of Pakistan image

you have to change it in datasource, DataTable or Object List
if you are using Data Table then use computational Column and if you are using objects the add a new property wich concatinate the other two.
Avatar of JCWEBHOST
JCWEBHOST

ASKER

yes, i am using datatable, can you please show me how to concatinate? in C# code?
you are getting data from databas
ethen change the query like this

Select name + '(' + surname + ')' as name from tablename

so that you will get data in name column itself no need to change .netcode
i do not want to mess around with the query.


 you want to show two data in a dropdownlist, you can combine two columns into 1 column and assign to the DataTextField
see the example

daAdapter = new SqlDataAdapter("SELECT course_type, course_num FROM tbl_course_offerings WHERE..........",con);
daAdapter.Fill(dsCourse,"Course");
 
dcolFullCourse.ColumnName = "FullCourse";
dcolFullCourse.DataType = System.Type.GetType("System.String");
dcolFullCourse.Expression = "course_type + ' ' + course_num";
 
dsCourse.Tables["Course"].Columns.Add(dcolFullCourse);
 
ddlCourse.DataSource - dsCourse.Tables["Course"].DefaultView;
ddlCourse.DataTextField = "FullCourse";
ddlCourse.DataValueField = "course_num";
can i use textformatstring?
ASKER CERTIFIED SOLUTION
Avatar of Imran Javed Zia
Imran Javed Zia
Flag of Pakistan image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Works great!!!