Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox within a Datagrid 1

Status
Not open for further replies.

dvannoy

MIS
May 4, 2001
2,765
US
I'm trying to add a listbox to a datagridview. I've created a item template within the grid now I'm trying to find the control so I can bind the list to a sql table. I'm getting e.item is not a member of system.eventargs. what am I missing? I've also tried rowdata bound, load and row created and still keep getting the same error.

Thanks

Private Sub dg_DataBound(sender As Object, e As System.EventArgs) Handles dg.DataBound


Dim list As ListBox = CType(e.item.findcontrol("list1"), ListBox)


End Sub

 
You have to use the rowdatabound event
Code:
 DirectCast(e.Row.FindControl("list1"),listbox)
 
okay,

I'm still getting an error....Object reference not set to an instance of an object

Private Sub dg_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles dg.RowDataBound

Dim myListBox As ListBox = DirectCast(e.Row.FindControl("list"), ListBox)

myListBox.BackColor = Drawing.Color.Aquamarine

End Sub



 
did you check the row type as the rowdatabound event fires?

Select Case e.Row.RowType
Case DataControlRowType.DataRow
'Do your stuff here

end Select
 
Thanks for this.... Now, Do I really need to bind the grid and then bind the listbox separate ?

 
Now, Do I really need to bind the grid and then bind the listbox separate ?
Yes. It won't do it automatically unless you use the DataSource controls which I don't suggest you ever use.

Your grid will bind to the datasource you provide, but it has no idea what you want to do with the listbox in the template column.
So, you have to then find the control, set the datasource and bind it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top