分配下拉列表文本值 [英] assign dropdownlist text value
问题描述
您好
i有一个下拉列表,它被数据绑定到一个数据表
然后我有另一个数据加载了用户以前的数据保存
第一张表包含银行列表
第二张表包含银行''命名之前选择的用户
第一张和第二张表的数据都是从数据库中检索的
i希望第二张表中的银行名称是下拉列表中显示的第一个。
我已经尝试过这个
dropDownlist1.DataSource = table1;
dropDownlist1.DataBind();
dropDownlist1.text = table2.Rows [ 0 ] [ 0 ]。ToString(); // 此项目应显示在下拉列表中
下拉列表中显示的项目没有变化,它始终是其数据源中显示的第一项。
非常感谢。 :)
抱歉编辑太多了。
我真的很难表达自己。
你好,
试试这段代码,
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);
}
dropDownlist1.DataSource = dt_clone;
dropDownlist1.DataBind();
问候,
Prakash.T
您好b $ b
这很简单。只需将数据绑定方法更改为下拉列表而不是直接绑定。
只需写入循环方法并在下拉列表中添加项目。
我知道这需要额外的努力,但它将满足您的要求。
< pre lang =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(); //
}
这不完美,有语法问题。如果您需要完整的解决方案,请告诉我们/>
这个公平的想法。
我用这种方法解决了这个问题
< pre lang =c#> dropDownlist1.DataSource = table1;
dropDownlist1.DataBind();
foreach (ListItem itm in dropDownlist1.Items)
{
if (itm.Text == table2.Rows [ 0 ] [ 6 ]。 ToString())
{
dropDownlist1.Text = itm.Value;
}
}
感谢回复..
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
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.
hi,
try this code,
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); }
dropDownlist1.DataSource = dt_clone; dropDownlist1.DataBind();
regards,
Prakash.T
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.
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.
i have solved this using this approach
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..
这篇关于分配下拉列表文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!