1

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?

Community
  • 1
  • 1
Nyra
  • 859
  • 1
  • 7
  • 27
  • To add additional info, the listbox itself has the correct displayed indexes (example "123456" or "654987"). It is the txt_BackDate_DateCode.Text that has the incorrect info inside it ("[Namespace].backDatePosting") – Nyra Jun 02 '14 at 17:37

1 Answers1

5

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"));
Thanatos
  • 136
  • 4
  • I don't understand. If I move the bindings before the source is populated, I get nothing populated. If I were to try and move it before the List<> is declared I will get a build error (I won't be able to reference it before it's declared). Am I missing something you are saying? – Nyra Jun 02 '14 at 19:23
  • 1
    You still need to populate your source list (lst variable) before setting it as the DataSource, but declare it as your DataSource after declaring the Display and Value members. – Thanatos Jun 02 '14 at 19:26
  • BOOM! Answered on point- Thank you so much that was REALLY annoying me :P – Nyra Jun 02 '14 at 19:29