0

I have the following code to populate a DataGridViewComboBoxColumn

        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

it populates the combobox fine. I have a button to insert new record into DataGridViewComboBoxColumn which open another form FormB and prompt the user to enter the name and ID for the new item. But the problem is that the newly added item do not appear in the comboBox until I close and re-open FormA. here is my code in FormB:

            BindingSource bs = new BindingSource();
            DataGridViewComboBoxColumn itemCmbClmn = prcFrm.itemIDcmbColmn; //Get FormsA DataGridViewComboBoxColumn
            DataTable itemDataTable = prcFrm.ItemData; //Get FormA datatable

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

            bs.DataSource = itemDataTable;
            itemCmbClmn.DataSource = bs;

            prcFrm.dataGridView1.Update();

When I run the addNew I got an exception that Field doesn't exist. Any help please, I am new to C# and visual studio, thank you in advance

jotnarta
  • 1
  • 1
  • Do You have one main form, another, amodal form formA and a third amodal form formB ? Is it like this ? – icbytes Dec 01 '15 at 12:37
  • FormA is the main form that contains a datagrid which contains DataGridViewComboBoxColumn , and the second form FormB is the form shown when the addNewItem Button clicked. No third form – jotnarta Dec 01 '15 at 12:58
  • Ok. Then make FormB throw an event. You should not update mainform-elements from other forms directly, nor should You create cross references. Instead You create an own event on form b, and You subscribe to it, when opening form B. You throw the event on form B, (custom event args can also be used to pass the new record to be added ) down to form A. The subsscribed eventhandler on formA will then have to update the grid. Try this. Ok ? – icbytes Dec 01 '15 at 13:01
  • I created a new instance of DataGridViewComboBox and add FormA's instance to the new one, it solved the exception, but still, the newly added value do not appear until I close and re-open FormA, any help please? – jotnarta Dec 01 '15 at 13:07
  • Thank you, but can you please post a sample code of doing this? because I am a newbie on C# and its events .. Thank you again. – jotnarta Dec 01 '15 at 13:09

1 Answers1

0
  1. MainForm is the Mainform.cs
  2. formB is the instance of FormB.cs
  3. FormB is the class "FormB.cs"

In FormB:

public event ItemAdded ItemAddedNotify;

#region OwnEventsAndDelegates
    public delegate void ItemAdded(object sender, ItemAddedEventArgs e);

    public class ItemAddedEventArgs : EventArgs
    {
        string _valueMember;
        public string ValueMember
        {
            get { return _ValueMember; }
            set { _ValueMember = value; }
        }

        string _displaymember;
        public string DisplayMember
        {
            get { return _displayMember; }
            set { _displayMember = value; }
        }

        public ItemAddedEventArgs(string pValueMember, string pDisplaymember)
        {
            _valueMember = pValueMember;
            _displayMember = pDisplaymember;
        }
    }

    #endregion

Later,in FormB, where You now do update(), You simply raise the event, instead of the update:

        ItemAddedNotify(this,new ItemAddedEventArgs(itemID.ToString(), txtItemAr.Text));

BUT. But when You open the formB, You must subscribe to the event. In the Mainform, after instantiation of formB, but before showing it, You should do:

this.formB.ItemAddedNotify += new FormB.ItemAddedNotify(On_ItemAdded);

The method

On_ItemAdded(object sender, FormB.ItemAddedEventArgs e)
{
  // Here You will have to add the stuff.
  // IT MIGHT BE BETTER TO USE TYPED DATASETS/TABLES.
  // It also might be, that the record will be visible ONLY after
  // closing formB. Please let me know.
}

must also exist in Your MainForm.

And do not to forget to unsubscribe from the event, after closing formB, the equivalent to +=, but with -=.

icbytes
  • 1,831
  • 1
  • 17
  • 27
  • Thank you very much, but How this could affect the data table of ComboBox in FormA? – jotnarta Dec 01 '15 at 14:14
  • The code is not ready yet, I will have to complete it, am busy now, will finish this snippet ASAP. – icbytes Dec 01 '15 at 14:15
  • Now it should do the work, as long as Your formB is amodal ( as I think, now). – icbytes Dec 01 '15 at 15:08
  • Thank you dear, I have put the code as you suggested, and make the required modificcations, but I didn't understand where to put ItemAddedNotify(this,new ItemAddedEventArgs(itemID.ToString(), txtItemAr.Text)); – jotnarta Dec 01 '15 at 16:03
  • where to put ItemAddedNotify(this,new ItemAddedEventArgs(itemID.ToString(), txtItemAr.Text)); – jotnarta Dec 01 '15 at 17:25
  • Replace your snippet of formB with this. – icbytes Dec 02 '15 at 05:50