Back to Devexpress

ASPxGridBase.FindVisibleIndexByKeyValue(Object) Method

aspnet-devexpress-dot-web-dot-aspxgridbase-dot-findvisibleindexbykeyvalue-x28-system-dot-object-x29.md

latest7.5 KB
Original Source

ASPxGridBase.FindVisibleIndexByKeyValue(Object) Method

Returns the data item (row, card or record) visible index by its key value.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public int FindVisibleIndexByKeyValue(
    object keyValue
)
vb
Public Function FindVisibleIndexByKeyValue(
    keyValue As Object
) As Integer

Parameters

NameTypeDescription
keyValueObject

An object that uniquely identifies the row.

|

Returns

TypeDescription
Int32

An integer value that specifies the data item visible index. The ASPxGridView.InvalidRowIndex value if the row is not found.

|

Remarks

The FindVisibleIndexByKeyValue method searches for the specified data item (row, card or record) in all the data items displayed within the grid. If the data item is not found, the FindVisibleIndexByKeyValue method returns the ASPxGridView.InvalidRowIndex field value.

Example

This example shows how to focus a row that is not displayed within the current page. To do this, switch to the page that contains the required row, and then move row focus.

csharp
using DevExpress.Web.ASPxGridView;

...

protected void Button1_Click(object sender, EventArgs e) {
    // Obtain the visible index of the required row.
    int rowIndex = grid.FindVisibleIndexByKeyValue("OLDWO");
    if (rowIndex == ASPxGridView.InvalidRowIndex) return;
    if (!IsRowVisibleOnScreen(rowIndex)) {
        // Switch to the page that contains the required row.
        GoToPage(rowIndex);
    }
    // Focus the required row.
    grid.FocusedRowIndex = rowIndex;
}
bool IsRowVisibleOnScreen(int rowIndex) {
    int startIndex = grid.PageIndex * grid.SettingsPager.PageSize;
    int endIndex = startIndex + grid.SettingsPager.PageSize;
    return rowIndex >= startIndex && rowIndex < endIndex;
}
void GoToPage(int rowIndex) {
    grid.PageIndex = rowIndex / grid.SettingsPager.PageSize;
}
vb
Imports DevExpress.Web.ASPxGridView
...
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    ' Obtain the visible index of the required row.
    Dim rowIndex As Integer = grid.FindVisibleIndexByKeyValue("OLDWO")
    If rowIndex = ASPxGridView.InvalidRowIndex Then Return
    If Not IsRowVisibleOnScreen(rowIndex) Then
        GoToPage(rowIndex)
    End If
    ' Focus the required row.
    grid.FocusedRowIndex = rowIndex
End Sub

Private Function IsRowVisibleOnScreen(ByVal rowIndex As Integer) As Boolean
    Dim startIndex As Integer = grid.PageIndex * grid.SettingsPager.PageSize
    Dim endIndex As Integer = startIndex + grid.SettingsPager.PageSize
    Return rowIndex >= startIndex AndAlso rowIndex < endIndex
End Function

Private Sub GoToPage(ByVal rowIndex As Integer)
    grid.PageIndex = rowIndex / grid.SettingsPager.PageSize
End Sub

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

Composite Keys

If the ASPxGridBase.KeyFieldName property contains a composite key, the FindVisibleIndexByKeyValue method can accept an array of key values as a parameter.

aspx
<dx:ASPxGridView ID="Grid" runat="server" AutoGenerateColumns="False" DataSourceID="GridDataSource" KeyFieldName="OrderID;ProductName">
     <Columns>
          <dx:GridViewDataTextColumn FieldName="OrderID" VisibleIndex="0" />
          <dx:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="1" />
          <dx:GridViewDataTextColumn FieldName="Supplier" VisibleIndex="2" />
     </Columns>
</dx:ASPxGridView>
csharp
int KeyIndex = Grid.FindVisibleIndexByKeyValue(new Object[] { 10249, "Tofu" });
vb
Dim KeyIndex As Integer = Grid.FindVisibleIndexByKeyValue(New Object() { 10249, "Tofu" })

An alternative way is to specify the key values that are separated by the vertical bar sign ( | ) within a string.

aspx
<dx:ASPxGridView ID="Grid" runat="server" AutoGenerateColumns="False" DataSourceID="GridDataSource" KeyFieldName="OrderID;ProductName">
     <Columns>
          <dx:GridViewDataTextColumn FieldName="OrderID" VisibleIndex="0" />
          <dx:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="1" />
          <dx:GridViewDataTextColumn FieldName="Supplier" VisibleIndex="2" />
     </Columns>
</dx:ASPxGridView>
csharp
int KeyIndex = Grid.FindVisibleIndexByKeyValue("10249|Tofu");
vb
Dim KeyIndex As Integer = Grid.FindVisibleIndexByKeyValue("10249|Tofu")

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the FindVisibleIndexByKeyValue(Object) method.

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.

aspxgridview-edit-in-memory-dataset/CS/WebApp/Default.aspx.cs#L74

csharp
protected void MasterGridView_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) {
    int i = MasterGridView.FindVisibleIndexByKeyValue(e.Keys[MasterGridView.KeyFieldName]);
    Control c = MasterGridView.FindDetailRowTemplateControl(i, "ASPxGridView2");

aspxgridview-edit-in-memory-dataset/VB/WebApp/Default.aspx.vb#L86

vb
Protected Sub MasterGridView_RowDeleting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataDeletingEventArgs)
    Dim i As Integer = MasterGridView.FindVisibleIndexByKeyValue(e.Keys(MasterGridView.KeyFieldName))
    Dim c As Control = MasterGridView.FindDetailRowTemplateControl(i, "ASPxGridView2")

See Also

GetRowValues(Int32, String[])

Grid View

ASPxGridBase Class

ASPxGridBase Members

DevExpress.Web Namespace