protected virtual void CommitProposedChanges(DataSet ds) { if (ds == null) return; for (int nTable = 0; nTable < ds.Tables.Count; nTable++) { for (int nRow = 0; nRow < ds.Tables[nTable].Rows.Count; nRow++) { if (ds.Tables[nTable].Rows[nRow].HasVersion(DataRowVersion.Proposed)) { ds.Tables[nTable].Rows[nRow].EndEdit(); } } } }I have since changed the way we do this, by putting a call to a static method in a Control's Validated event handler, but depending on the way you do your DataBinding, you may or may not be able to use that. The above CommitProposedChanges method worked great, although it may be time consuming for large datasets with many rows.
public static void EndEdit(Binding oBinding, CurrencyManager oCurrency) { if (oBinding == null || oBinding.DataSource == null || oCurrency == null) return; EndEdit(oBinding.DataSource, oCurrency.Position); } public static void EndEdit(object DataSource, int nRow) { if (nRow < 0) return; // Originally, I was going to simply use the .EndCurrentEdit() of the // Currency object, but for certain controls (the ComboBox being the first // one where I noticed a problem), the .EndCurrentEdit() caused the // DataSet.HasChanges() to be true when there aren't any changes, whereas // the Row.EndEdit() did not cause this to happen. // A simple check as to whether the Row.HasVersion(DataRowVersion.Proposed) // before doing the .EndCurrentEdit() would get around this, but since I would // still have to do the work to get the Row out of the DataSource (because the // DataSource can be a DataTable or a DataView), I might as well use .EndEdit() if (DataSource is DataTable && nRow < ((DataTable)DataSource).Rows.Count) ((DataTable)DataSource).Rows[nRow].EndEdit(); else if (DataSource is DataView && nRow < ((DataView)DataSource).Count) ((DataView)DataSource)[nRow].EndEdit(); }~~Bonnie
>public bool UpdateDataSet(DataSet oDataSet, string sTableName) >{ > bool bRetVal = true; > > try > { > oAdapter.Update(oDataSet, sTableName); > } > catch(SqlException e) > { > oException = e; > bRetVal = false; > } > return bRetVal; >} >>
>foreach(DataTable oTable in oDataSet.Tables) >{ > foreach(DataRow oRow in oTable.Rows) > { > oRow.EndEdit(); > } >} >>
>> DataRow oRow = oDataSet.Tables[0].Rows[0].EndEdit();
>>
>> oAdapter.Update(oDataSet, sTableName);
>>
>>>>>txtTitle.DataBindings.Add("Text", oDataSet, "ItemRow.Title"); >>>txtCategory.DataBindings.Add("Text", oDataSet, "ItemRow.Category"); >>>txtAuthor.DataBindings.Add("Text", oDataSet, "ItemRow.Author"); >>>txtCatNo.DataBindings.Add("Text", oDataSet, "ItemRow.Catalogue_No"); >>>txtIsbn.DataBindings.Add("Text", oDataSet, "ItemRow.Isbn"); >>>txtPublisher.DataBindings.Add("Text", oDataSet, "ItemRow.Publisher"); >>>>>>
>>>public bool UpdateDataSet(DataSet oDataSet, string sTableName) >>>{ >>> bool bRetVal = true; >>> >>> try >>> { >>> DataRow oRow = oDataSet.Tables[0].Rows[0]; >>> DataRowState state = oRow.RowState; \\state = Unchanged >>> >>> oAdapter.Update(oDataSet, sTableName); >>> } >>> catch(SqlException e) >>> { >>> oException = e; >>> bRetVal = false; >>> } >>> return bRetVal; >>>} >>>>>>