Given: DataGridView is bound to a BindingSource, BindingSource is bound to a table from a DataSet, DataSet gets data from a TableAdapter.

At DGV on the Event CellEndEdit "hang": "Tab1TableAdapter.Update (dataSet1);" those. after cell editing is completed we update the data in the database. But this only works if we change the value in the cell and click on another cell. If, after changing the value, press Enter, the data in the database will not be updated. Even if you create a separate button with "Tab1TableAdapter.Update (dataSet1);" nothing will happen. It is possible that the DGV does not understand that the data in it has been changed and does not transmit data to the Dataset, and therefore the update fails. Please tell me how to be.

Form1.cs

private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'dataSet1.TAB1' table. You can move, or remove it, as needed. this.tAB1TableAdapter.Fill(this.dataSet1.TAB1); } private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { tAB1TableAdapter.Update(dataSet1); } 


Form1.Designer.cs

 private void InitializeComponent(){ ... ... ... this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.dataSet1 = new WindowsFormsApplication1.DataSet1(); this.tAB1BindingSource = new System.Windows.Forms.BindingSource(this.components); this.tAB1TableAdapter = new WindowsFormsApplication1.DataSet1TableAdapters.TAB1TableAdapter(); // // dataGridView1 // this.dataGridView1.AutoGenerateColumns = false; this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.iDDataGridViewTextBoxColumn, this.cLDataGridViewTextBoxColumn}); this.dataGridView1.DataSource = this.tAB1BindingSource; this.dataGridView1.Location = new System.Drawing.Point(24, 62); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.Size = new System.Drawing.Size(478, 317); this.dataGridView1.TabIndex = 0; this.dataGridView1.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellEndEdit); // // dataSet1 // this.dataSet1.DataSetName = "DataSet1"; this.dataSet1.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema; // // tAB1BindingSource // this.tAB1BindingSource.DataMember = "TAB1"; this.tAB1BindingSource.DataSource = this.dataSet1; // // tAB1TableAdapter // this.tAB1TableAdapter.ClearBeforeFill = true; ... ... ... } 
  • Give the code - otherwise you will not get a clear answer. - Silento

2 answers 2

Solution found.

 private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { dataGridView1.EndEdit(); tAB1TableAdapter.Update(dataSet1); } 

    Yes, the CellEndEdit event CellEndEdit occurs after the cell loses focus. You need to additionally handle the CurrentCellDirtyStateChanged event.

    • Unfortunately, it's not an event, I tried to force a call to tAB1TableAdapter.Update (dataSet1); using the button, the data in the database does not receive the changed values. Everything works fine if after editing the cell, click on another cell. But if you use the Enter key or click on any other item, and also call _tAB1TableAdapter.Update (dataSet1); then nothing happens. - Halva
    • @Halva - I do not believe it! (c) Maybe your click handler is not attached to the button? It seems to me that the reason is banal - the challenge of something has been forgotten somewhere. - Alexander Petrov