Click here to Skip to main content
15,914,066 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there

i have a dropdownlist wich is databinded to a datatable
then i have this other datable loaded with data the user previously saved

the first table contains the list of banks

the second table contains the bank's name the user previously selected

the data for the first and second table are both retrieved from the database

i want the bank's name from the second table to be the first 1 displayed in the dropdownlist.

i've already try this



C#
dropDownlist1.DataSource = table1;
dropDownlist1.DataBind();
dropDownlist1.text = table2.Rows[0][0].ToString();//this item should be the one displayed on the dropdownlist


the displayed item in the dropdownlist doesn't change, it's always the first item from its datasource that is being displayed.


thanks a lot. :)

sorry for too much editing.
it's really hard for me to express myself.
Posted
Updated 25-Feb-13 18:58pm
v3
Comments
Sandeep Mewara 26-Feb-13 0:25am    
This is not a well framed question! We cannot work out what you are trying to do/ask from the post. Please rephrase.
Use the "Improve question" link to edit your question and provide better information.
ZaneArellano 26-Feb-13 0:38am    
is it clear now?
Sandeep Mewara 26-Feb-13 0:43am    
No.

You have 2 datatables. How are they populated?
What do you mean by 'now i want to assign the value's of the 2nd datable to their respective dropdownlist text property'?

Your question is not clear on what you have and what are you trying to do.

hi,

try this code,


C#
DataTable dt_clone = new DataTable();
dt_clone = dt2.Clone();

foreach (DataRow dr_Clone in dt2.Rows)
{
    dt_clone.ImportRow(dr_Clone);
    break;
}

foreach (DataRow dr_Clone in dt1.Rows)
{
    dt_clone.ImportRow(dr_Clone);
}


C#
dropDownlist1.DataSource = dt_clone;
dropDownlist1.DataBind();


regards,
Prakash.T
 
Share this answer
 
Hi
This is pretty simple. Just change your approach of databinding to dropdownlist rather than directly binding.
Just write for loop method and add items in dropdownlist.
I know this need extra effort, but it will fulfill your requirement.
C#
for (int i = 0; i < datatable.Rows.Count; i++)
{
      DropDownList1.Items.Add(new ListItem(table2.Rows[i][0].ToString(),table2.Rows[i][0].ToString();//
}


This is not perfect, has syntax problem. If you need complete solution, let me know
this just fair idea.
 
Share this answer
 
v2
i have solved this using this approach

C#
dropDownlist1.DataSource = table1;
dropDownlist1.DataBind();

 foreach (ListItem itm in dropDownlist1.Items)
 {
   if (itm.Text == table2.Rows[0][6].ToString())
   {
      dropDownlist1.Text = itm.Value;
   }
 }


thanks for the replies..
 
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