Nyra
Nyra

Reputation: 897

Listbox SelectedValue wrong on SelectedIndex first call

I did try finding other answers first- I found this (Items on ListBox show up as a class name) post to be similar but didn't answer exactly...

Anyways, I have a listbox and for some reason it is populating the class name as the selected value, but only when it goes through loading for the first time (I did step through in debug, and it calls the selectedindex changed when the list gets populated. The values are correct, as well as the display names, but it takes the object ToString(). I do not want to override the ToString() Method in the backDatePosting class unless there is a way to differentiate the two fields (ie both are strings and I need them to return accordingly)

the backDatePosting class object

public class backDatePosting
{
    private readonly string _matnum;
    private readonly string _dtCode;

    public string MatNum
    {
        get
        {
            return _matnum;
        }
    }
    public string DateCode
    {
        get
        {
            return _dtCode;
        }
    }

    public backDatePosting(string _matnum, string _dtCode)
    {
        this._matnum = _matnum;
        this._dtCode = _dtCode;
    }
}

and then my form

    public Form1()
    { 
      ......
      refreshBackDatePosting();
    }

    void refreshBackDatePosting()
    {
        DataTable dt; 

        System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("sample conn string");
        using (conn)
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("development_usp_getBackDatePosting", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            using(cmd)
            {
                dt = new DataTable();
                conn.Open();
                System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
                dt.Load(dr);
                conn.Close();
            }
        }

        List<backDatePosting> lst = new List<backDatePosting>();
        for (int i = 0; i < dt.Rows.Count; i++ )
        {
            lst.Add(new backDatePosting(dt.Rows[i][0].ToString().Trim(), dt.Rows[i][1].ToString().Trim()));
        }
        this.lst_BackDatePosting.DataSource = lst;
        this.lst_BackDatePosting.DisplayMember = "MatNum";
        this.lst_BackDatePosting.ValueMember = "DateCode";
    }

and the selected index change event

    void lst_BackDatePosting_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        txt_BackDate_DateCode.Text = lst_BackDatePosting.SelectedValue.ToString();
        //throw new System.NotImplementedException();
    }

What am I missing?

Upvotes: 1

Views: 480

Answers (1)

Thanatos
Thanatos

Reputation: 136

The issue here is that you're setting your DataSource before you set up your binding information (DisplayMember and ValueMember). Even though the SelectedIndexChanged event fires, at that time the SelectedValue is the entire backDatePosting object, which is why you get the [Namespace].backDatePosting string. There are two ways to fix this:

1) Declare your binding before your DataSource:

    this.lst_BackDatePosting.DisplayMember = "MatNum";
    this.lst_BackDatePosting.ValueMember = "DateCode";
    this.lst_BackDatePosting.DataSource = lst; // Now follows the binding

2) Setup a new binding that bypasses the SelectedIndexChanged event and instead uses the actual SelectedValue:

    txt_BackDate_DateCode.DataBindings.Add(new Binding("Text", lst_BackDatePosting, "SelectedValue"));

Upvotes: 5

Related Questions