Back to Devexpress

ASPxGridView.FocusedRowIndex Property

aspnet-devexpress-dot-web-dot-aspxgridview-86992ada.md

latest5.9 KB
Original Source

ASPxGridView.FocusedRowIndex Property

Specifies the index of the focused row.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public int FocusedRowIndex { get; set; }
vb
Public Property FocusedRowIndex As Integer

Property Value

TypeDescription
Int32

The focused row index.

|

Remarks

ASPxGridView identifies rows by their indices. The FocusedRowIndex property specifies the index of the focused row.

Set the AllowFocusedRow property to true to enable row focus. Change the FocusRowIndex property value to move focus to the corresponding row within the current page. When the row focus changes, the server-side ASPxGridView.FocusedRowChanged or the client-side ASPxClientGridView.FocusedRowChanged event fires (based on the ProcessFocusedRowChangedOnServer property value). If you navigate to another page, the row loses focus.

When row focus is disabled, the FocusedRowIndex property returns -1. To identify the row being edited, use the EditingRowVisibleIndex property.

Web Forms Example

The following example shows how to use the FocusedRowIndex property:

aspx
<dx:ASPxGridView ID="gridView" runat="server" AutoGenerateColumns="False" KeyFieldName="CategoryID"
    OnDataBound="gridView_DataBound" OnFocusedRowChanged="gridView_FocusedRowChanged">
    <SettingsBehavior AllowFocusedRow="true" ProcessFocusedRowChangedOnServer="true" />
    <Columns>
        <dx:GridViewDataTextColumn FieldName="CategoryID" ReadOnly="True" VisibleIndex="0">
            <EditFormSettings Visible="False" />
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="CategoryName" VisibleIndex="1" />
        <dx:GridViewDataTextColumn FieldName="Description" VisibleIndex="2" />
    </Columns>
</dx:ASPxGridView>
csharp
protected void gridView_FocusedRowChanged(object sender, EventArgs e) {
    ASPxGridView grid = sender as ASPxGridView;
    Response.Cookies[grid.ID]["FocusedIndex"] = grid.FocusedRowIndex.ToString();
    Response.Cookies[grid.ID].Expires = DateTime.Now.AddDays(1d);
}
protected void gridView_DataBound(object sender, EventArgs e) {
    ASPxGridView grid = sender as ASPxGridView;
    if(!IsPostBack)
    if (Request.Cookies[grid.ID] != null)
        grid.FocusedRowIndex =Convert.ToInt32(Request.Cookies[grid.ID]["FocusedIndex"]);
}

MVC Example

cshtml
@Html.DevExpress().GridView(settings => {
    settings.Name = "GridView";
    settings.CallbackRouteValues = new { Controller = "Home", Action = "GridViewPartial" };
    settings.SettingsPager.PageSize = 5;

    settings.KeyFieldName = "EmployeeID";
    settings.Columns.Add("LastName");
    settings.Columns.Add("FirstName");
    settings.Columns.Add("City");
    settings.Columns.Add("Country");

    settings.PreRender = (s, e) => {
        var sender = (MVCxGridView)s;
        // Set the zero-based index of the focused row.  
        sender.FocusedRowIndex = 6;
    };
    // Enable row focus.  
    settings.SettingsBehavior.AllowFocusedRow = true;
}).Bind(Model).GetHtml()

Online Examples

View Example: Grid View for ASP.NET Web Forms - How to focus the newly inserted row

View Example: Grid View for ASP.NET MVC - How to focus the newly inserted row

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the FocusedRowIndex property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

asp-net-web-forms-grid-lookup-access-selected-data/CS/ASPxGridLookUpSelection/ServerSide_SingleSelect.aspx.cs#L11

csharp
ASPxGridView grid = ASPxGridLookup1.GridView;
object value = grid.GetRowValues(grid.FocusedRowIndex, new string[] { "ProductName" });
ASPxListBox1.Items.Clear();

asp-net-web-forms-grid-lookup-access-selected-data/VB/ASPxGridLookUpSelection/ServerSide_SingleSelect.aspx.vb#L14

vb
Dim grid As ASPxGridView = ASPxGridLookup1.GridView
Dim value As Object = grid.GetRowValues(grid.FocusedRowIndex, New String() {"ProductName"})
ASPxListBox1.Items.Clear()

See Also

ASPxGridView Class

ASPxGridView Members

DevExpress.Web Namespace