why error with RowDataBound event and not with RowCreated?

  • Thread starter Thread starter Ben
  • Start date Start date
B

Ben

Hi,

Can someone explain me why the same code with the event
GridView1_RowDataBound gives this error:

'DropDownList1' has a SelectedValue which is invalid because it does not
exist in the list of items.
Parameter name: value

and not with the event GridView1_RowCreated ?

Thanks
Ben

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As

System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
If (e.Row.RowState And DataControlRowState.Edit) =
DataControlRowState.Edit Then
If e.Row.RowType = DataControlRowType.DataRow Then
Dim dd As DropDownList
Dim i As Integer
Dim z As ListItem
dd = e.Row.FindControl("DropDownList1")
For i = 1 To 20
z = New ListItem(i, i)
dd.Items.Add(z)
Next
End If
End If
End Sub
 
By this code. The reason is that the selectedvalue in the aspx file requires
too early a value which is only provided at rowdatabound event.

Eliyahu Goldin said:
Is the error caused by this code or by some other code or settings not
included in your post? In the former case, on what line? How does the
template look like?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Ben said:
Hi,

Can someone explain me why the same code with the event
GridView1_RowDataBound gives this error:

'DropDownList1' has a SelectedValue which is invalid because it does not
exist in the list of items.
Parameter name: value

and not with the event GridView1_RowCreated ?

Thanks
Ben

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As

System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
If (e.Row.RowState And DataControlRowState.Edit) =
DataControlRowState.Edit Then
If e.Row.RowType = DataControlRowType.DataRow Then
Dim dd As DropDownList
Dim i As Integer
Dim z As ListItem
dd = e.Row.FindControl("DropDownList1")
For i = 1 To 20
z = New ListItem(i, i)
dd.Items.Add(z)
Next
End If
End If
End Sub
 
Back
Top