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.
Free Contact Form Generator for Expression Web - http://www.ctrfx.com
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.
Free Contact Form Generator for Expression Web - http://www.ctrfx.com
aspdotnettwo
Member
152 Points
135 Posts
FindControl in Gridview RowDataBound event
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.
e_screw
All-Star
19530 Points
3894 Posts
Re: FindControl in Gridview RowDataBound event
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
Try this:
If e.Row.RowType = DataControlRowType.DataRow Then
Dim sql as SqlDataSource = e.Row.FindControl("SqlDataSource1")
End If
Thanks
Electronic Screw
Website||Blog||Dub@i.net
aspdotnettwo
Member
152 Points
135 Posts
Re: FindControl in Gridview RowDataBound event
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.
e_screw
All-Star
19530 Points
3894 Posts
Re: FindControl in Gridview RowDataBound event
Apr 06, 2007 01:55 PM|LINK
If you are trying the datakey values in the RowDataBound event, then use this
Dim _
key As String = GridView1.DataKeys(e.Row.RowIndex).Value.ToString()Thanks
Electronic Screw
Website||Blog||Dub@i.net
aspdotnettwo
Member
152 Points
135 Posts
Re: FindControl in Gridview RowDataBound event
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?
thanks
e_screw
All-Star
19530 Points
3894 Posts
Re: FindControl in Gridview RowDataBound event
Apr 06, 2007 04:22 PM|LINK
Try this :
Dim key as String = GridView1.DataKeys(e.Row.RowIndex)(1).Value.ToString()
Thanks
Electronic Screw
Website||Blog||Dub@i.net
aspdotnettwo
Member
152 Points
135 Posts
Re: FindControl in Gridview RowDataBound event
Apr 06, 2007 05:43 PM|LINK
Error message: Public member 'Value' on type 'String' not found.
Thank you so much for your time
e_screw
All-Star
19530 Points
3894 Posts
Re: FindControl in Gridview RowDataBound event
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()
Thanks
Electronic Screw
Website||Blog||Dub@i.net
aspdotnettwo
Member
152 Points
135 Posts
Re: FindControl in Gridview RowDataBound event
Apr 06, 2007 06:31 PM|LINK