DataGridViewComboBoxColumn not updating

两盒软妹~` 提交于 2020-01-17 06:34:52

问题


I have ProcurementFrm that list the available items in a

DataGridViewComboBoxColumn 

using this method:

  /**
       Populate combo box in datagrid in procurement screen
      */
    public void populateGridComboBox()
    {
        con.Open();
        MySqlCommand sc = new MySqlCommand("select id, ar_desc from minierp_db.items", con);
        //MySqlDataReader reader;
        MySqlDataReader itemReader = null;

        try
        {
            itemReader = sc.ExecuteReader();

            itemDT = new DataTable();

            itemDT.Columns.Add("id", typeof(string));
            itemDT.Columns.Add("ar_desc", typeof(string));
            itemDT.Load(itemReader);

            itemIDcmbColmn.ValueMember = "id";
            itemIDcmbColmn.DisplayMember = "ar_desc";
            itemIDcmbColmn.DataSource = itemDT;
        }
        catch (Exception e)
        {
            MessageBox.Show("Exception - populateGridComboBox(): " + e.Message);
        }
        finally
        {
            con.Close();
            itemReader.Dispose();
        }
    } //populateGridComboBox

and in the same form I have a button that call another form addItemFrm, which enables user from adding a new item. The item is added correctely but it's not reflected directely in the ComboBoxColumn until I close and reopen the ProcurementFrm.

Here how I add the new Item to ComboBoxColumn

  public void populateNewAddedItems()
    {
        procurmentsFrm prcFrm = (procurmentsFrm)Application.OpenForms["procurmentsFrm"];
        MySqlConnection connn = new MySqlConnection("datasource=127.0.0.1;port=3306;username=root;password=admin");
        MySqlCommand sc = new MySqlCommand("select id, ar_desc from minierp_db.items", connn);


        //itemDT = new DataTable();
        try
        {
            DataGridViewComboBoxColumn itemCmbClmn = new DataGridViewComboBoxColumn();


            itemCmbClmn.ValueMember = itemID.ToString();
            itemCmbClmn.DisplayMember = txtItemAr.Text;

            BindingSource bs = new BindingSource();
            DataTable itemDataTable = prcFrm.ItemData;
            bs.DataSource = itemDataTable;
            itemCmbClmn.DataSource = bs;

Any Help Please? I want the comboBox to get updated directely.

来源:https://stackoverflow.com/questions/33787389/datagridviewcomboboxcolumn-not-updating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!