我给了一个嵌套网格。当点击子网格中的按钮时,我必须得到父网格行索引。我使用过这种类型的代码
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "go")
{
GridViewRow Gv2Row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
GridView Childgrid = (GridView)(Gv2Row.Parent.Parent);
GridViewRow Gv1Row = (GridViewRow)(Childgrid.NamingContainer);
GridView Parentgrid = (GridView)(Gv1Row.Parent.Parent);
}
}
但是它不起作用.. 显示类型转换错误。怎么解决..
答案 0 :(得分:0)
请尝试使用以下代码段。
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "go")
{
GridViewRow Gv2Row = (GridViewRow)((LinkButton)sender).NamingContainer;
GridView Childgrid = (GridView)(Gv2Row.Parent.Parent);
GridViewRow Gv1Row = (GridViewRow)(Childgrid.NamingContainer);
int b = Gv1Row.RowIndex;
// Access your parent grid index here
}
}
答案 1 :(得分:0)
您在下面的评论行中错过了额外的(
和)
,导致typecasting
错误。并且您不需要代码中的最后一个语句,因为您已找到父网格。
我建议您最好更改代码中parent/child gridview/row
对象的命名约定,以便让它更好地理解,详见下文。
if (e.CommandName == "go")
{
// GridViewRow Gv2Row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
GridViewRow childrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
GridView parentgrid = (GridView)(childrow.Parent.Parent);
GridViewRow parentrow = (GridViewRow)(parentgrid.NamingContainer);
int parentrowindex = parentrow.RowIndex;
}