0

I am working on web app with data base (SQL server). I made my multiple drop down list and now what I need is when I select one value from it to show only first column.

Something like this:

id ,      Text

31231 dadsadsa

32131 dsadsads

54345 dasdadsd

53455 trretrer

When I select the first row 31231 dadsadsa, I want to appear only the id of it (31231).

I use this to bind these two columns

for (int i = 0; i < dt.Rows.Count; i++)
{
    id = dt.Rows[i]["first_c"].ToString();
    name = dt.Rows[i]["second_c"].ToString();
    newName = id + " ---- " + name;
    DropDownList1.Items.Add(new ListItem(newName, id));
}

I hope you guys understand me.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
Milan
  • 1
  • 1
  • Maybe get the int from the DropDownList value? http://stackoverflow.com/questions/4734116/find-and-extract-numbers-from-a-string – Pavenhimself Sep 22 '14 at 08:43
  • what is the problem with your code? what do you see with it and what do you want to see? – Pedro.The.Kid Sep 22 '14 at 11:05
  • When i choose from drop down list one value, in the drop down list appears the whole row(newName value), instead only id value. I am searching for something like DisplayMember in Combo. – Milan Sep 23 '14 at 08:39

2 Answers2

0

have you tried this ?

DropDownList1.DataValueField ="id";
DropDownList1.DataTextField="newName";
Nurdin
  • 23,382
  • 43
  • 130
  • 308
MasterBettor
  • 99
  • 1
  • 1
  • 9
  • i think that this 2 answers are same like my solution. I tried them but it doesnt work. Again, when i choose from drop down list one value, in the drop down box appears the whole row(newName value), instead only id value – Milan Sep 22 '14 at 10:40
0
List<SelectListItem> DropDownList1 = new List<SelectListItem>();

for (int i = 0; i < dt.Rows.Count; i++)
{
    id = dt.Rows[i]["first_c"].ToString();
    name = dt.Rows[i]["second_c"].ToString();
    newName = id + " ---- " + name;

    DropDownList1.Add(new SelectListItem() { Text = id, Value = newName}); 
}
asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84
  • i think that this 2 answers are same like my solution. I tried them but it doesnt work. Again, when i choose from drop down list one value, in the drop down box appears the whole row(newName value), instead only id value – Milan Sep 22 '14 at 10:38