网上没有找到解决方案,试了N次,终于解决了。这里暂时提供2种方案:
// 选中某一行
public static void FocusGridRowByKeyValue(DevExpress.Web.ASPxGridView.ASPxGridView grv, string sKeyFieldValue)
{
grv.SettingsBehavior.AllowFocusedRow = false;
grv.SettingsBehavior.AllowFocusedRow = true;
grv.FocusedRowIndex = -1;
grv.Selection.SelectRow(grv.FindVisibleIndexByKeyValue(sKeyFieldValue));
bool bBackToPage = grv.MakeRowVisible(sKeyFieldValue);// 可以定位回到某一页去
}
// 选中某一行
public static void FocusGridRowByKeyValue(DevExpress.Web.ASPxGridView.ASPxGridView grv, string sKeyFieldName, string sKeyFieldValue)
{
object oValue = null;
for (int i = 0; i < grv.VisibleRowCount; i++)
{
oValue = grv.GetRowValues(i, sKeyFieldName);
if (oValue != null && oValue.ToString() == sKeyFieldValue)
{
grv.SettingsBehavior.AllowFocusedRow = false;
grv.SettingsBehavior.AllowFocusedRow = true;
grv.FocusedRowIndex = -1;
grv.Selection.SelectRow(i);
bool bBackToPage = grv.MakeRowVisible(sKeyFieldValue);// 可以定位回到某一页去
break;
}
}
}
调用例子:
if(Request.Params["id"] != null && Request.Params["id"].Trim() !="")
{
FocusGridRowByKeyValue(grv_User, "UserID", Request.Params["id"].ToString());
}
或
if(Request.Params["id"] != null && Request.Params["id"].Trim() !="")
{
FocusGridRowByKeyValue(grv_User,Request.Params["id"].ToString());
}