none
ComboBox is not Refreshed RRS feed

  • Question

  • Hello,

    I have a combobox in my one form like City name, And I have Master form for that so I defined fill city combo public so i can call it on form close event of that master...

    but combobox is not refreshed with new add or delete value...

    Here is my code..

    string qry = "Select * from City";
            dtcity = cc.GetDataSection(qry, connection);
            cmbCity.DataSource = null;
            cmbCity.DisplayMember = "CityName";
            cmbCity.ValueMember = "CityId";
            cmbCity.DataSource = dtcity ;

    I am getting perfect data in datatable but not in combo box....

    Any help would be appreciate..

    Thank u...

    • Moved by CoolDadTx Monday, July 25, 2016 4:53 PM Winforms related
    Monday, July 25, 2016 5:35 AM

Answers

  • If you did something like this

    using (OleDbCommand cmd = new OleDbCommand { CommandText = "SELECT Identifier,ActiveMember, UserName, UserRole, JoinMonth FROM Users", Connection = cn })
    {
        OleDbDataReader dr = cmd.ExecuteReader();
        dtUsers.Load(dr);
        cboUsers.DisplayMember = "UserName";
        cboUsers.DataSource = dtUsers;
    }

    You would use the following syntax to add a row that will then display in the ComboBox. Of course I used mocked data, no reason why you could not say grab data from controls on a form.

    ((DataTable)cboUsers.DataSource).Rows.Add(new object[] { null,true, "Harry" });

    There is one issue, the new item is added to the bottom, we can change this by setting the default view sort property e.g.

    OleDbDataReader dr = cmd.ExecuteReader();
    dtUsers.Load(dr);
    dtUsers.DefaultView.Sort = "UserName ASC";
    cboUsers.DisplayMember = "UserName";
    cboUsers.DataSource = dtUsers;


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, July 25, 2016 9:52 AM
    Moderator