Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,776
    Thanks
    41
    Thanked 196 Times in 195 Posts

    Listbox SelectedValue wrong on SelectedIndexChanged first call

    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
    Code:
    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
    Code:
    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

    Code:
    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?


    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")
    Last edited by alykins; 06-02-2014 at 08:30 PM. Reason: Resolved

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,776
    Thanks
    41
    Thanked 196 Times in 195 Posts
    Answer- Set DisplayName and DisplayValue before setting binding.

    Code:
    this.lst_BackDatePosting.DisplayMember = "MatNum";
    this.lst_BackDatePosting.ValueMember = "DateCode";
    this.lst_BackDatePosting.DataSource = lst;

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •