简体   繁体   中英

c# combobox has a datasource, how do i make it display a specific element?

i have a datatable as the source of a combobox:

1
3
2
4
5

without knowing the order of the elemnts inside the datatable, and only knowing the exact text, is it possible to display a specific element like ' 4 ' ?

so you should do this:

comboBox1.Text = "4";

or change the value of related field in DataTable:

((MyDataRowType)((DataRowView)bindingSource1.Current).Row).myFieldName = "4";

In DataBound event of your dropdownlist try this:

protected void DropDownList1_DataBound(object sender, EventArgs e)
{
    for (int i = 0; i < DropDownList1.Items.Count; i++)
    {
        if (DropDownList1.Items[i].Text == "4")
        {
            DropDownList1.SelectedIndex = i;
        }
    }

}
myComboBox.SelectedItem = "4";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM