Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 06, 2007 06:31 PM by aspdotnettwo
Apr 06, 2007 12:41 PM|LINK
I can find my SQL datasource in my gridview by using the following code, like, for example, in a page load event:
Dim sql As SqlDataSource = Me.GridView1.Rows(3).FindControl("SqlDataSource1")
But I need to be able to find that control on the GridView1_RowDataBound event, and this code does not work. "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"
Any help wold be GREATLY appreciated.
Apr 06, 2007 01:09 PM|LINK
RowDataBound event is for the current row being bounded in the GridView. You should refer to the current binding row with, e.Row.FindControl
If e.Row.RowType = DataControlRowType.DataRow Then
Dim sql as SqlDataSource = e.Row.FindControl("SqlDataSource1")
Apr 06, 2007 01:48 PM|LINK
THANK YOU THANK YOU THANK YOU! THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU!!!!
One more question:
How would I pull out a field in the DataKeyNames property of my gridview for each row. Does that make any sense? I want to get the unique piece of data in a field for each row.
Apr 06, 2007 01:55 PM|LINK
If you are trying the datakey values in the RowDataBound event, then use this
Apr 06, 2007 02:06 PM|LINK
I have two fields in DataKeyNames property and I want to pull out the second one. How do I specify which field?
Apr 06, 2007 04:22 PM|LINK
Try this :
Dim key as String = GridView1.DataKeys(e.Row.RowIndex)(1).Value.ToString()
Apr 06, 2007 05:43 PM|LINK
Error message: Public member 'Value' on type 'String' not found.
Thank you so much for your time
Apr 06, 2007 05:48 PM|LINK
I am sorry, it should be this way
Dim key as String = GridView1.DataKeys(e.Row.RowIndex)(1).ToString()
Apr 06, 2007 06:31 PM|LINK