I have a gridview with paging enabled and a textbox where the user can enter a string value used to select the row in the gridview with a column that matches the string value. The column’s data is not the primary key and is not listed in the datakeynames.
I’ve found an example that solves the problem for a non-paging gridview. In an event handler for a button control associated with the textbox:
protected void SelFrame_Clicked(object sender, EventArgs e)
{
string requestedFrame = tbSelFrame.Text;
if (requestedFrame != "")
{
int index = (from DataControlField col in gvFrame.Columns
where col.HeaderText == "Name"
select gvFrame.Columns.IndexOf(col)).FirstOrDefault();
var query = from GridViewRow row in gvFrame.Rows
where row.Cells[index].Text == requestedFrame
select row;
GridViewRow result = query.FirstOrDefault();
if (result != null)
{
int selectedIndex = result.DataItemIndex;
if (selectedIndex > -1)
gvFrame.SelectedIndex = selectedIndex;
}
}
}
As noted, when the value being searched is on a non-visible page of the gridview, this line returns null:
GridViewRow result = query.FirstOrDefault();
How can I find the matching row in a gridview with paging enabled?
You should add the value as a datakey instead, and then do something like this: