ASP.Net 2.0 : how to set the selectedrow

  • Thread starter Thread starter Steve B.
  • Start date Start date
S

Steve B.

Hi,

I'm building an ASP.Net 2.0 web application that uses the gridview control
(read only).
This gridview is filled by a DataTable which is the result of the select
method of an object binding source (which is a WebService but I don't think
it matters).

I also have a querystring parameter "id" and I want to "Select" the row of
the gridview that match one of its column (which is the primary key of my
table).

I did not succeed in setting the .SelectedRow since it's a readonly
property. I also tryed to set the selectedindex of the gridview, but I don't
know the index of the row before the databind.

How can I reach my goal ?

Thanks in advance,
Steve
 
Steve,

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If GridView1.Rows(e.Row.RowIndex - 1).Cells(1).Text = "Search Text"
Then
e.Row.RowState = DataControlRowState.Selected
End If
End If
End Sub

The above code in the code behind page should do the job. Change the
index of the Cells collection and the value you are check for to decide
if the row is selected or not.

The GridView must be configured with Enable Selection otherwise you
will get an error.

Regards,
Steve
 
I was confused because the XML documentation of the property RowState is
"Get the ...." and not "Gets or sets the..." like the documentation
convention is supposed to be...

Thanks for this lighting !
 
Back
Top