一直使用DevExpress控件包,现在经过摸索已经基本熟悉了它的使用,但昨天在做一个GridControl的Master-Detail样式(样式如下)时还是遇到了问题。
![]()
问题是这样的:我需要在子表中根据鼠标右键点击的具体行,获取当前行的数据。问题看似很简单,以前也写过类似的功能,代码如下:
![]()
Code
GridHitInfo hi = this.gvBetMaster.CalcHitInfo(this.gcBetMasterDetail.PointToClient(Cursor.Position));
if (hi.Column != null && hi.InRowCell)
{
DataRow currentBunch = this.gvBetMaster.GetDataRow(hi.RowHandle);
string bunchID = currentBunch["串号"].ToString();
tsmnuiDelBunch.Text = "删除[" + bunchID + "]";
tsmnuiDelBunch.Tag = bunchID;
this.tsmnuiDelBunch.Visible = true;
}
else
{
this.tsmnuiDelBunch.Visible = false;
}
但是在这里却始终无法获得hi.RowHandle,最后我通过下面的方法使问题得到了解决:
![]()
Code
GridView gv=this.gcBetMasterDetail.GetViewAt(e.Location) as GridView;
GridHitInfo hi = gv.CalcHitInfo(e.Location);
if (hi.Column != null && hi.InRowCell)
{
DataRow currentMatch = gv.GetDataRow(hi.RowHandle);
string vs = currentMatch["主队"] + " VS " + currentMatch["客队"];
this.tsmnuiReplaceMatch.Text = "替换比赛[" + vs + "]";
this.tsmnuiReplaceMatch.Tag = currentMatch["流水号"];
this.tsmnuiReplaceMatch.Visible = true;
}
else
{
this.tsmnuiReplaceMatch.Visible = false;
}
也就是说,这里你不可以再直接使用你设计时的GridView名字对行进行访问,而是要实时获取当前鼠标点击的GridView。欢迎大家讨论!