aspnet-devexpress-dot-web-dot-aspxgridbase-dot-findvisibleindexbykeyvalue-x28-system-dot-object-x29.md
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
public int FindVisibleIndexByKeyValue(
object keyValue
)
Public Function FindVisibleIndexByKeyValue(
keyValue As Object
) As Integer
| Name | Type | Description |
|---|---|---|
| keyValue | Object |
An object that uniquely identifies the row.
|
| Type | Description |
|---|---|
| Int32 |
An integer value that specifies the data item visible index. The ASPxGridView.InvalidRowIndex value if the row is not found.
|
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.
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.
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;
}
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
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
If the ASPxGridBase.KeyFieldName property contains a composite key, the FindVisibleIndexByKeyValue method can accept an array of key values as a parameter.
<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>
int KeyIndex = Grid.FindVisibleIndexByKeyValue(new Object[] { 10249, "Tofu" });
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.
<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>
int KeyIndex = Grid.FindVisibleIndexByKeyValue("10249|Tofu");
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
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
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