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

Jobs from Indeed

Issue accessing ListBox directly after data binding

Issue accessing ListBox directly after data binding

(OP)
I am trying to access a ListBox right after I data bind to it and am getting an error that says it is empty. I assume it doesn't do it right away and is done somewhere down the line because the ListBox has data when the form comes back.

In the following code the SelectList has data in it when I binds to the ListBox. But gives an error if you try to query it.

CODE

public List<StatementItem> SelectList { get; set; }

...

SelectList = (from DataRow dr in SelectStatements.Tables[0].Rows
                select new StatementItem
                {
                    Statement = (string)dr["Statement"],
                    StatementName = (string)dr["StatementName"],
                    DBStatementID = dr["DBStatementID"].ToString()
                }).ToList();

lbStatementNames.DisplayMember = "StatementName";
lbStatementNames.ValueMember = "DBStatementID";
lbStatementNames.DataSource = SelectList;

txtSelectStatement.Text = ((StatementItem)SelectList.First(x => x.DBStatementID == (string)lbStatementNames.Items[0])).Statement; 

The error I get from the last statement is:

InvalidArgument=Value of '0' is not valid for 'index'.

if you look at the lbStatementNames.Items.Count, it is 0. But SelectList.Count = 5.

When the form comes back it has 5 items in it.

At which point can I query this ListBox after the data binding?

Thanks,

Tom

RE: Issue accessing ListBox directly after data binding

(OP)
I also tried to set the selection to the 3rd item:

CODE

lbStatementNames.DisplayMember = "StatementName";
lbStatementNames.ValueMember = "DBStatementID";
lbStatementNames.DataSource = SelectList;

lbStatementNames.SelectedItem = 2; 

I didn't get an error but the 1st item was selected. I assume that was because there were no items in the listbox yet.

I can't find anywhere where anyone talks about the point at which the object is populated or how to get it populate immediately.

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