I have the following littel sub that fires when GridView1 row created:
Protected Sub xxxxx(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
Dim C_ID As String = GridView1.SelectedRow.Cells(0).Text
GetC_AttributesObjectDataSource.InputParameters("C_ID") = C_ID
GetC_AttributesObjectDataSource.DataBind()
End Sub
The problem is, there is an error, as some of you may see GetC_AttributesObjectDataSource.InputParameters("C_ID") = C_ID is the wrong part.
As an example, GetC_AttributesObjectDataSource.InputParameters("C_ID") = C_ID will only work with the correct method signature which is:
Protected Sub GetC_AttributesObjectDataSource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles GetC_AttributesObjectDataSource.Selecting
Dim C_ID As String = GridView1.SelectedRow.Cells(0).Text
GetC_AttributesObjectDataSource.DataBind()
e.InputParameters("C_ID") = C_ID
End Sub
Well , the second one works but not on GridView1.RowCreated, instead it works with GetC_AttributesObjectDataSource.Selecting, which does not help me.
How can I do my proposed operation with the GridView1.RowCreated event?
I'm not exactly sure what you're trying to do, but by the time the RowCreated event has fired, the GridView has already been bound to the ObjectDataSource. If you need to modify a Parameter of the ObjectDataSource, then the ObjectDataSource.Selecting event
is the proper place for this.
Protected Sub GridViewC_Definition_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each row As GridViewRow In GridViewC_Definition.Rows
DimC_ID As String
C_ID = row.Cells(0).Text
TextBox1.Text = C_ID
GetC_AttributesObjectDataSource.Select()
Next
End Sub
Let me explain again. As you can see here, as soon as the page loads my sub is iterating through the rows that are created by the gridview and gets the C_ID from each and
writes it into a textbox - this textbox is then used by my GetC_AttributesObjectDataSource (not shown) as the parameter required for the select() statement. Unfortunately , the only ID in the textbox at the end is the one of the last row, because all the onces before are overwritten. and thats my problem.
The same happens when I bind the whole think to the ObjectDataSource.Selecting method, as you proposed
Protected Sub GetC_AttributesObjectDataSource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles GetC_AttributesObjectDataSource.Selecting Dim C_ID As String = GridView1.SelectedRow.Cells(0).Text GetC_AttributesObjectDataSource.DataBind() e.InputParameters("C_ID") = C_ID End Sub
Here, it does not even iterate through the rows, - which makes this method also ineffective because there is no iteration through each row.
To conclude and built on your suggestion, I need to modify a Parameter of the ObjectDataSource, every time a GridView1 row is created (that is where the parameter comes from), then the ObjectDataSource.Selecting event is the proper place for this.
PS: the parameter that feeds into the objectdatasource is then used to select specific information for each C_ID that was in the GridView1.Row (datakey)
Sorry, but this just doesn't make any sense to me. In your example, you're firing off the Select method of the ObjectDataSource for every row in your GridView. This is highly ineffecient as you'd only see the results for the last row in your GridView both
in your TextBox and the results of the secondary ObjectDataSource. You should only be data-binding to a particular ObjectDataSource once.
you are right, it would usually be efficient to fire the objectdatasource.select only once. but that is how my table/gridview looks:
So I have to re-bind the GetC_AttributesObjectDataSource (which is the datasource for the DropDownList) everytime a row is created since it takes the freshly created C_ID as a paramter for the
GetC_AttributesObjectDataSource that then fills the dropdownlist with the specific values for the specific C_ID.
Or.....is there something I am not really aware about ?
I think I'm understanding what you're doing now. Each row has a DropDownList in it which is tied to one ObjectDataSource which is keyed off of the ID from each row. If this is the case, then you should move your code to the GridView.RowDataBound event. In
that event, retireve a reference to your DropDownList and ObjectDataSource. Retrieve the ID from the row and assign it properly to your ObjectDataSource. Then, call the DataBind method of your DropDownList.
I have done it your way and came up with this sub:
Protected Sub GridViewC_Definition_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewC_Definition.RowDataBound
' 1. Retrieve reference from DropDownList
Dim C_ID As String
Dim GridViewTemplateC_ValuesDropDownList = TryCast(GridViewC_Definition.FindControl("DropDownListC_Values"), DropDownList)
' 2. Retrieve ID from each row
For Each row As GridViewRow In GridViewC_Definition.Rows
C_ID = row.Cells(0).Text
' 3. Reference from GetC_AttributesObjectDataSource
'GetC_Attributes.xxxx
'4. Assign ID from row and assign to the objectdatasource
GetC_Attributes.SelectParameters("C_ID").DefaultValue = C_ID
Next
'5. call data bind method from dropdownlist
'GridViewTemplateC_ValuesDropDownList.DataBind() <- if I include this i.e. remove the ' than an error appears: Object reference not set to an instance of an object.
End Sub
THe only problem now is that all the dropdownlists have the same value
Protected Sub GridViewC_Definition_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewC_Definition.RowDataBound
' 1. Retrieve reference from DropDownList
Dim C_ID As String
Dim GridViewTemplateC_ValuesDropDownList = TryCast(GridViewC_Definition.FindControl("DropDownListC_Values"), DropDownList)
' 2. Retrieve ID from each row
For Each row As GridViewRow In GridViewC_Definition.Rows
C_ID = row.Cells(0).Text
' 3. Reference from GetC_AttributesObjectDataSource
'GetC_Attributes.xxxx
'4. Assign ID from row and assign to the objectdatasource
GetC_Attributes.SelectParameters("C_ID").DefaultValue = C_ID
Next
'5. call data bind method from dropdownlist
'GridViewTemplateC_ValuesDropDownList.DataBind() <- if I include this i.e. remove the ' than an error appears: Object reference not set to an instance of an object.
End Sub
THe only problem is second row bind the ObjectDataSource the first results in the dropdownList are overwritten by the second bind and it goes on i.e. the third row assigns a value to the
ObjectDataSource and all DropDOwnLists (in the template field) are overwritten again.
The RowDataBound event is fired when every row in the GridView is DataBound so there's no need to further iterate through all of the rows. Do something more like this:
Protected Sub GridViewC_Definition_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewC_Definition.RowDataBound
Dim gvr As GridViewRow = e.Row
If gvr.RowType = DataControlRowType.DataRow Then
Dim GridViewTemplateC_ValuesDropDownList = CType(gvr.FindControl("DropDownListC_Values"), DropDownList)
GetC_Attributes.SelectParameters("C_ID").DefaultValue = gvr.Cells(0).Text
GridViewTemplateC_ValuesDropDownList.DataBind()
End If
End Sub
Thanks, Ed
Microsoft MVP - ASP.NET
Marked as answer by polynaux on Jan 31, 2008 05:27 PM
Thanks for your suggestion. I would have never come up with this kind of code.
Interestingly, the dropdownlist values are mixed up completely now.
Whereas the DropDownList in Row 1 has the 2 values correct values + the two values from the next row/DropDownList
DropDownList in Row 2 has the 2 values from the first row, the correct values, + the 2 values from row 3
finally DropDownList in row 3 has the 3 values from row 2 and its own 2 correct values.
How on earth can that be ?
many thanks again,
polynaux
PS: the sql query works correctly
PS2 : maybe the error has to do with the
...gvr.FindControl("DropDownListC_Values")... <- since every dropdownlist in the rows is called "DropDownListC_Values", and that is why they are added up (the values)
polynaux
Member
195 Points
775 Posts
Incorrect method signature: ObjectDataSource funktionality for the GridView.RowCreated event
Jan 30, 2008 10:44 AM|LINK
Hi Guys,
I have the following littel sub that fires when GridView1 row created:
Protected Sub xxxxx(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated Dim C_ID As String = GridView1.SelectedRow.Cells(0).Text GetC_AttributesObjectDataSource.InputParameters("C_ID") = C_ID GetC_AttributesObjectDataSource.DataBind() End SubThe problem is, there is an error, as some of you may see
GetC_AttributesObjectDataSource.InputParameters("C_ID") = C_ID is the wrong part.
As an example, GetC_AttributesObjectDataSource.InputParameters("C_ID") = C_ID will only work with the
correct method signature which is:
Well , the second one works but not on GridView1.RowCreated, instead it works with GetC_AttributesObjectDataSource.Selecting, which does not help me.
How can I do my proposed operation with the GridView1.RowCreated event?
many thanks,
polynaux
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: Incorrect method signature: ObjectDataSource funktionality for the GridView.RowCreated event
Jan 30, 2008 12:30 PM|LINK
I'm not exactly sure what you're trying to do, but by the time the RowCreated event has fired, the GridView has already been bound to the ObjectDataSource. If you need to modify a Parameter of the ObjectDataSource, then the ObjectDataSource.Selecting event is the proper place for this.
Microsoft MVP - ASP.NET
polynaux
Member
195 Points
775 Posts
Re: Incorrect method signature: ObjectDataSource funktionality for the GridView.RowCreated event
Jan 30, 2008 12:49 PM|LINK
HI ecbruck !
Protected Sub GridViewC_Definition_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each row As GridViewRow In GridViewC_Definition.Rows DimC_ID As String C_ID = row.Cells(0).Text TextBox1.Text = C_ID GetC_AttributesObjectDataSource.Select() Next End SubLet me explain again. As you can see here, as soon as the page loads my sub is iterating through the rows that are created by the gridview and gets the C_ID from each and
writes it into a textbox - this textbox is then used by my GetC_AttributesObjectDataSource (not shown) as the parameter required for the select() statement.
Unfortunately , the only ID in the textbox at the end is the one of the last row, because all the onces before are overwritten. and thats my problem.
The same happens when I bind the whole think to the ObjectDataSource.Selecting method, as you proposed
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: Incorrect method signature: ObjectDataSource funktionality for the GridView.RowCreated event
Jan 30, 2008 01:11 PM|LINK
Sorry, but this just doesn't make any sense to me. In your example, you're firing off the Select method of the ObjectDataSource for every row in your GridView. This is highly ineffecient as you'd only see the results for the last row in your GridView both in your TextBox and the results of the secondary ObjectDataSource. You should only be data-binding to a particular ObjectDataSource once.
Microsoft MVP - ASP.NET
polynaux
Member
195 Points
775 Posts
Re: Incorrect method signature: ObjectDataSource funktionality for the GridView.RowCreated event
Jan 30, 2008 01:36 PM|LINK
Hi ecbruck,
you are right, it would usually be efficient to fire the objectdatasource.select only once. but that is how my table/gridview looks:
So I have to re-bind the GetC_AttributesObjectDataSource (which is the datasource for the DropDownList) everytime a row is created since it takes the freshly created C_ID as a paramter for the
GetC_AttributesObjectDataSource that then fills the dropdownlist with the specific values for the specific C_ID.
Or.....is there something I am not really aware about ?
Thanks for your help,
polynaux
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: Incorrect method signature: ObjectDataSource funktionality for the GridView.RowCreated event
Jan 30, 2008 01:43 PM|LINK
I think I'm understanding what you're doing now. Each row has a DropDownList in it which is tied to one ObjectDataSource which is keyed off of the ID from each row. If this is the case, then you should move your code to the GridView.RowDataBound event. In that event, retireve a reference to your DropDownList and ObjectDataSource. Retrieve the ID from the row and assign it properly to your ObjectDataSource. Then, call the DataBind method of your DropDownList.
Microsoft MVP - ASP.NET
polynaux
Member
195 Points
775 Posts
Re: Incorrect method signature: ObjectDataSource funktionality for the GridView.RowCreated event
Jan 30, 2008 02:41 PM|LINK
Hi ecbruck !
I have done it your way and came up with this sub:
Protected Sub GridViewC_Definition_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewC_Definition.RowDataBound ' 1. Retrieve reference from DropDownList Dim C_ID As String Dim GridViewTemplateC_ValuesDropDownList = TryCast(GridViewC_Definition.FindControl("DropDownListC_Values"), DropDownList) ' 2. Retrieve ID from each row For Each row As GridViewRow In GridViewC_Definition.Rows C_ID = row.Cells(0).Text ' 3. Reference from GetC_AttributesObjectDataSource 'GetC_Attributes.xxxx '4. Assign ID from row and assign to the objectdatasource GetC_Attributes.SelectParameters("C_ID").DefaultValue = C_ID Next '5. call data bind method from dropdownlist 'GridViewTemplateC_ValuesDropDownList.DataBind() <- if I include this i.e. remove the ' than an error appears: Object reference not set to an instance of an object. End Subpolynaux
Member
195 Points
775 Posts
Re: Incorrect method signature: ObjectDataSource funktionality for the GridView.RowCreated event
Jan 31, 2008 08:11 AM|LINK
THat is my latest "creation"
Protected Sub GridViewC_Definition_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewC_Definition.RowDataBound ' 1. Retrieve reference from DropDownList Dim C_ID As String Dim GridViewTemplateC_ValuesDropDownList = TryCast(GridViewC_Definition.FindControl("DropDownListC_Values"), DropDownList) ' 2. Retrieve ID from each row For Each row As GridViewRow In GridViewC_Definition.Rows C_ID = row.Cells(0).Text ' 3. Reference from GetC_AttributesObjectDataSource 'GetC_Attributes.xxxx '4. Assign ID from row and assign to the objectdatasource GetC_Attributes.SelectParameters("C_ID").DefaultValue = C_ID Next '5. call data bind method from dropdownlist 'GridViewTemplateC_ValuesDropDownList.DataBind() <- if I include this i.e. remove the ' than an error appears: Object reference not set to an instance of an object. End SubTHe only problem is second row bind the ObjectDataSource the first results in the dropdownList are overwritten by the second bind and it goes on i.e. the third row assigns a value to the
ObjectDataSource and all DropDOwnLists (in the template field) are overwritten again.
HOw can I avoid this overwriting ?
have a great day,
polynaux
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: Incorrect method signature: ObjectDataSource funktionality for the GridView.RowCreated event
Jan 31, 2008 12:22 PM|LINK
The RowDataBound event is fired when every row in the GridView is DataBound so there's no need to further iterate through all of the rows. Do something more like this:
Protected Sub GridViewC_Definition_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewC_Definition.RowDataBound Dim gvr As GridViewRow = e.Row If gvr.RowType = DataControlRowType.DataRow Then Dim GridViewTemplateC_ValuesDropDownList = CType(gvr.FindControl("DropDownListC_Values"), DropDownList) GetC_Attributes.SelectParameters("C_ID").DefaultValue = gvr.Cells(0).Text GridViewTemplateC_ValuesDropDownList.DataBind() End If End SubMicrosoft MVP - ASP.NET
polynaux
Member
195 Points
775 Posts
Re: Incorrect method signature: ObjectDataSource funktionality for the GridView.RowCreated event
Jan 31, 2008 04:28 PM|LINK
Hi ecbruck !
Thanks for your suggestion. I would have never come up with this kind of code.
Interestingly, the dropdownlist values are mixed up completely now.
Whereas the DropDownList in Row 1 has the 2 values correct values + the two values from the next row/DropDownList
DropDownList in Row 2 has the 2 values from the first row, the correct values, + the 2 values from row 3
finally DropDownList in row 3 has the 3 values from row 2 and its own 2 correct values.
How on earth can that be ?
many thanks again,
polynaux
PS: the sql query works correctly
PS2 : maybe the error has to do with the
...gvr.FindControl("DropDownListC_Values")... <- since every dropdownlist in the rows is called "DropDownListC_Values", and that is why they are added up (the values)