简体   繁体   English

将gridview中的选定行传递到下拉列表框

[英]Pass selected row in gridview to drop down list box

I am trying to pass a selected row in gridview to my dropdown. 我正在尝试将gridview中的选定行传递给我的下拉菜单。 I have 1 gridview, 1 textbox and 1 dropdown list box. 我有1个gridview,1个文本框和1个下拉列表框。 My dropdown is populated by SqlDataSource from my table Country. 我的下拉列表由表国家(地区)中的SqlDataSource填充。

On my gridview, I have 3 columns, which are SELECT, NAME and COUNTRY. 在我的gridview上,我有3列,分别是SELECT,NAME和COUNTRY。 Under SELECT column, I have my hyperlinked Select, every time I clicked on a certain row NAME will pass in textbox (no problem with that), but in my dropdown it does not populates the COUNTRY that I choose. 在“选择”列下,我有一个“选择”超链接,每次单击某行时,“名称”都会在文本框中传递(这没有问题),但是在我的下拉列表中,它不会填充我选择的“国家”。

For example, I have Jack for name and USA for country. 例如,我有杰克(Jack)为名字,美国(USA)为国家。 Beside Jack and USA, I have a hyperlink Select, when I click Select, Jack will display on my textbox and supposed to be USA will display on my dropdown but not displaying instead it says an error: 在Jack和USA旁边,我有一个超链接Select,当我单击Select时,Jack将显示在我的文本框中,而应该是USA的则显示在我的下拉列表中,但不显示,而是显示错误:

'cboCountry' has a SelectedValue which is invalid because it does not exist in the list of items. 'cboCountry'的SelectedValue无效,因为它在项目列表中不存在。 Parameter name: value 参数名称:值

I have tried the code below: 我试过下面的代码:

protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
   txtname.Text = GridView2.SelectedRow.Cells[1].Text;
   cboCountry.Text = GridView2.SelectedRow.Cells[2].Text;
}

Only the textbox is working, no luck at all in dropdown. 只有文本框有效,下拉菜单中根本没有运气。 I'm using C# and asp.net. 我正在使用C#和asp.net。

Try to use cboCountry.SelectedItem.Text .After getting the value set this thing to dropdown like 尝试使用cboCountry.SelectedItem.Text 。获得值后将此东西设置为dropdown

protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
   txtname.Text = GridView2.SelectedRow.Cells[1].Text;
   cboCountry.SelectedItem.Text = GridView2.SelectedRow.Cells[2].Text;
}

Hope it works 希望能奏效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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