当我们从gridview选择的行中获得相应的datatextfield时,如何将下拉列表中的项目显示为已选中

时间:2021-11-15 23:01:11

Code:

码:

DataSet ds = _dalEquipmentwiseCheckList.getEquipmentName();

ddEquipmentName.DataSource = ds.Tables[0].DefaultView;

ddEquipmentName.DataTextField = ds.Tables[0].Columns[1].ToString();
ddEquipmentName.DataValueField = ds.Tables[0].Columns[0].ToString();
ddEquipmentName.DataBind();

What I want is: when selecting a row in the GridView, the corresponding equipment name should get selected in the dropdown list:

我想要的是:在GridView中选择一行时,应在下拉列表中选择相应的设备名称:

var selectRow = MyGridView.SelectedRow;

var selectRow = MyGridView.SelectedRow;

ddEquipmentName.SelectedValue = selectRow.Cells[2].Text;
****//this is giving me error****

3 个解决方案

#1


1  

Selected value does not work in this way. Try this:

选择的值不能以这种方式工作。尝试这个:

ddEquipmentName.SelectedIndex = ddEquipmentName.Items.IndexOf(ddEquipmentName.Items.FindByText(selectRow.Cells[2].Text));

#2


0  

You can try :

你可以试试 :

GridViewRow selectRow = MyGridView.SelectedRow;    
ddEquipmentName.selectedItem.Text = selectRow.Cells[2].Text;

#3


0  

 void setDataContext()
{
    var selectRow = GridEquipmentChechList.MyGridView.SelectedRow;
    if (selectRow != null)
    {
        txtECNumber.Text = selectRow.Cells[1].Text;
        **ddEquipmentName.SelectedIndex = ddEquipmentName.Items.IndexOf(ddEquipmentName.Items.FindByText(selectRow.Cells[2].Text));**
        **ddDescription.SelectedIndex = ddDescription.Items.IndexOf(ddDescription.Items.FindByText(selectRow.Cells[3].Text));**
    }
}

#1


1  

Selected value does not work in this way. Try this:

选择的值不能以这种方式工作。尝试这个:

ddEquipmentName.SelectedIndex = ddEquipmentName.Items.IndexOf(ddEquipmentName.Items.FindByText(selectRow.Cells[2].Text));

#2


0  

You can try :

你可以试试 :

GridViewRow selectRow = MyGridView.SelectedRow;    
ddEquipmentName.selectedItem.Text = selectRow.Cells[2].Text;

#3


0  

 void setDataContext()
{
    var selectRow = GridEquipmentChechList.MyGridView.SelectedRow;
    if (selectRow != null)
    {
        txtECNumber.Text = selectRow.Cells[1].Text;
        **ddEquipmentName.SelectedIndex = ddEquipmentName.Items.IndexOf(ddEquipmentName.Items.FindByText(selectRow.Cells[2].Text));**
        **ddDescription.SelectedIndex = ddDescription.Items.IndexOf(ddDescription.Items.FindByText(selectRow.Cells[3].Text));**
    }
}