Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C ASP.NET
I have a DropDownList Control in my page,

i am binding both DataValueField and DataTextFiled to it.

For example: Employee details,

Data value Field =1
Data Text Field ="ABCD"

i want the text in dropdownlist will show like this

"1-ABCD"

and also i want to select the Data Value Field from this dropdownlist

thats 1 in above example

How can i do this


Please help
Posted 15-Feb-11 20:57pm
Edited 15-Feb-11 21:23pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Either create a custom object or get the textfield value as '1 - ABCD' from query itself.

For now, it looks like you are a beginner, try the Query way. When fetching the data, concatenate the ID and Text field and then use this field as the datatextfield while binding.

Sample:
SELECT 
  ID, TextF, (ID + '-' + TextF) AS IDTEXTF
FROM
  MyTable
mydropdown.Datasource = myFetchedDataTable;
mydropdown.DataValueField = "ID";
mydropdown.DataTextField = "IDTEXTF";
mydropdown.Databind();
  Permalink  
v2
Comments
kapilonkar at 4-Mar-13 1:53am
   
Can anyone Tell me if I do not want to pass DataTextField and DataValueField how can I bind DropDownList with datasource ?
At present I am getting bound Value as System.Data.DataRowView for all the Items.

Please Tell me If anyone knows the Solution.

Thanks in Advance.
Sandeep Mewara at 4-Mar-13 2:03am
   
You have to define the datatextfield and datavaluefield to map the field/value.


Further, next time, please post your questions as a new question instead of a comment to any question previously asked by someone else.
kapilonkar at 4-Mar-13 3:33am
   
Yes Sure,

Thanks for Information Sandeep.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,534
1 OriginalGriff 6,258
2 Peter Leow 2,534
3 Maciej Los 2,308
4 Abhinav S 2,274


Advertise | Privacy | Mobile
Web03 | 2.8.150414.1 | Last Updated 16 Feb 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100