INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Listbox within a Datagrid

Listbox within a Datagrid

(OP)
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

RE: Listbox within a Datagrid

You have to use the rowdatabound event

CODE

DirectCast(e.Row.FindControl("list1"),listbox) 

RE: Listbox within a Datagrid

(OP)
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

RE: Listbox within a Datagrid

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

RE: Listbox within a Datagrid

(OP)
Thanks for this.... Now, Do I really need to bind the grid and then bind the listbox separate ?

RE: Listbox within a Datagrid

Quote:

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close