Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Adding Records in DataGridView does not update underlying List<> object
 

Adding Records in DataGridView does not update underlying List<> object

I have an application where I am displaying a List<> object in a DataGridView. 

If I add an item to the list programmatically, I can edit that item in the DataGridView just fine, and any changes are reflected in the databound object.

However, if I have the DataGridView's AllowUserToAddRows property set to True, I can add rows in the DataGridView, but these items are not added to my underlying list.  The weird thing is, though, that I can delete an item view the DataGridView, and it is removed from the list.

Here is my List<> descendant class:
class
 Global
    {
        private
 string
 _Label = String.Empty;
        private
 string
 _Value = String.Empty;

        public
 string
 Label
        {
            get
 { return
 _Label; }
            set
 { _Label = value; }
        }        

        public
 string
 Value
        {
            get
 { return
 _Value; }
            set
 { _Value = value; }
        }

        public
 string
 Output
        {
            get
 { return
 String.Format("[{0}]={1}"
, _Label, _Value); }
        }

        public
 Global(string
 label, string
 value)
        {
            _Label = label;
            _Value = value;
        }
    }

    class
 Globals : List<Global>
    {
        public
 XElement Xml
        {
            get

            {
                XElement thisElement = new
 XElement("Globals"
);

                foreach
 (Global thisGlobal in
 this
)
                {
                    thisElement.Add(new
 XElement("Global"
, thisGlobal.Output));
                }

                return
 thisElement;
            }
        }
    }
And here is my binding code:

bindingSourceGlobals.DataSource = null
;
dataGridViewGlobals.DataSource = null
;
            
_Globals.Add(new
 Global("New Label"
, "New Value"
));

bindingSourceGlobals.DataSource = _Globals;
dataGridViewGlobals.DataSource = bindingSourceGlobals;
How can I force additions in the DataGridView to reflect in the underlying List<> object?

Thanks!

Gabe
  • Edited byGabe Covert 13 hours 56 minutes agoRealized I can delete items from the list
  •  
Gabe Covert  13 hours 58 minutes ago
Thanks for the help.

It turns out that the problem is with my base class, Global.  It did not have a parameter-less constructor, and DataGridView cannot add a new item to an underlying binding source list if the class doesn't have one.

Gabe
Gabe Covert  12 hours 20 minutes ago
Hi Gabe - If it is bind to your datagridview, force addition is not  necessary its like defeating the purpose of binding.
When are you calling the code of binding? 

I did write a simple program that is similar to your problem.
My objects are button and datagridview. The dgridViewTestStationEntry has True value with AllowUserToAddRows and AllowUserToDeleteRows.
public partial class Form1 : Form
    {
        private List<TestEquipment> testEquipmentCollection;

        public Form1()
        {
            InitializeComponent();
            LoadData();
        }

        private void LoadData()
        {
            testEquipmentCollection = new List<TestEquipment>();
            
            for (int i = 0; i < 5; i++)
			{
			    TestEquipment testEquipment = new TestEquipment();
                testEquipment.Address = "Address"+ i.ToString();
                testEquipment.Command = "Command" + i.ToString();
                testEquipmentCollection.Add(testEquipment);
			}
            
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = testEquipmentCollection;
            dgridViewTestStationEntry.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells ;
            dgridViewTestStationEntry.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            dgridViewTestStationEntry.DataSource = bindingSource;
                
        }

        private void button1_Click(object sender, EventArgs e)
        {

            //This will loop in your binding source
            if (testEquipmentCollection != null)
            {
                foreach (TestEquipment te in testEquipmentCollection)
                {
                    MessageBox.Show(te.Address + te.Command);
                }
            }
            
        }


    }

    public class TestEquipment
    {
        public string Address { get; set; }
        public string Command { get; set; }
    }

  • Proposed As Answer bya-caridad- 12 hours 56 minutes ago
  • Unproposed As Answer byGabe Covert 12 hours 20 minutes ago
  •  
a-caridad-  12 hours 58 minutes ago
Thanks for the help.

It turns out that the problem is with my base class, Global.  It did not have a parameter-less constructor, and DataGridView cannot add a new item to an underlying binding source list if the class doesn't have one.

Gabe
Gabe Covert  12 hours 20 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• formating column problem
• databinding combobox AFTER combobox has been displayed, has no effect
• SetColumnError changes the Postion of my dataView - why ?
• filling ComboBox and Sorting, for null values to be at 1st index
• Bound DataGridView Sorting: One-shot sorting
• Hierarchical datagrid from one datatable
• DataTable like binding
• programmatically find a row in a DataGridView and select it
• Find Dataset Column Types
• TabPages and BindingContexts