问题
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