c#dropdownlist selectedindex在gridview集合中更改了第二个dropdownlist的selectedindex

c#dropdownlist selectedindex在gridview集合中更改了第二个dropdownlist的selectedindex,c#,gridview,drop-down-menu,selectedindexchanged,C#,Gridview,Drop Down Menu,Selectedindexchanged,我有一个gridview,其中有两个从db填充的DropDownList。一个是描述性名称,另一个是缩写名称。我需要完成以下工作: protected void ddlAddLabTest_SelectedIndexChanged(object sender, EventArgs e) { DropDownList ddlLabTest = (DropDownList)sender; GridViewRow row = (GridViewRow)ddlLabTest.NamingCo

我有一个gridview,其中有两个从db填充的DropDownList。一个是描述性名称,另一个是缩写名称。我需要完成以下工作:

protected void ddlAddLabTest_SelectedIndexChanged(object sender, EventArgs e)
{
   DropDownList ddlLabTest = (DropDownList)sender;
   GridViewRow row = (GridViewRow)ddlLabTest.NamingContainer;
   DropDownList ddlAddLabTestShortName = (DropDownList)row.FindControl("ddlAddShortname");

   ddlAddLabTestShortName.SelectedIndex = intSelectedIndex;
}
Unable to cast object of type 'System.Web.UI.WebControls.DataGridItem' to type 'System.Web.UI.WebControls.GridViewRow'.
当我在DDL1中选择一个项目时,我需要更改DDL2的所选索引以匹配,反之亦然

我在此处搜索并发现以下内容:

protected void ddlAddLabTest_SelectedIndexChanged(object sender, EventArgs e)
{
   DropDownList ddlLabTest = (DropDownList)sender;
   GridViewRow row = (GridViewRow)ddlLabTest.NamingContainer;
   DropDownList ddlAddLabTestShortName = (DropDownList)row.FindControl("ddlAddShortname");

   ddlAddLabTestShortName.SelectedIndex = intSelectedIndex;
}
Unable to cast object of type 'System.Web.UI.WebControls.DataGridItem' to type 'System.Web.UI.WebControls.GridViewRow'.
只有当到达“行”的分配时,我才会收到以下信息:

protected void ddlAddLabTest_SelectedIndexChanged(object sender, EventArgs e)
{
   DropDownList ddlLabTest = (DropDownList)sender;
   GridViewRow row = (GridViewRow)ddlLabTest.NamingContainer;
   DropDownList ddlAddLabTestShortName = (DropDownList)row.FindControl("ddlAddShortname");

   ddlAddLabTestShortName.SelectedIndex = intSelectedIndex;
}
Unable to cast object of type 'System.Web.UI.WebControls.DataGridItem' to type 'System.Web.UI.WebControls.GridViewRow'.

我试着找到一个有效的例子,但我找不到。非常感谢您的帮助

似乎
NamingContainer
不是一行,所以请保持这样。它已经有了
FindControl
方法

var row = ddlLabTest.NamingContainer;
试试这个

protected void ddlAddLabTest_SelectedIndexChanged(object sender, EventArgs e)
{
   DropDownList ddlLabTest = (DropDownList)sender;
   DataGridItem row = (DataGridItem) ddlLabTest.NamingContainer;
   DropDownList ddlAddLabTestShortName = (DropDownList)row.FindControl("ddlAddShortname");

   ddlAddLabTestShortName.SelectedIndex = intSelectedIndex;
}

您是否尝试过javascript解决方案,或者是否需要回发?如果DDL总是彼此相等,为什么两者都需要?尝试过:“无法将类型”System.Web.UI.Controls“隐式转换为”System.Web.UI.WebControls.GridViewRow“。存在显式转换(是否缺少转换?)”删除GridViewRow类型。使用变量。非常感谢!我不得不使用GridViewRow而不是DataGridItem,但它成功了!:)