Question

I had a task to bind two DataGridView's, main problem I can't see the changes when I cast BindingSource data source to DataSet (I need this for SqlDataAdapter).

I can workaround this by deleting db table contents, but this is not suitable for me.

I could catch these events, but I need to update DB on disposing the Form.

var bs = (BindingSource)dataGridView1.DataSource;
var set = (DataSet)bs.DataSource; //here DataSet HasChanges() method always returns true

And when I use Tables from DataSet in SqlDataAdapter Update() and Fill() methods do the Insert for me, because values are always shown as new.

It is kind of though to explain it right, I hope I have succeeded.

Thanks.

EDIT

Fixed it by initializing DataSet on class level and adding AcceptChanges method. Also removed casting from BindingSource.

Code snippet for initialization of DataSet:

dataSetT = new DataSet();
            dataSetT.ReadXmlSchema(xmlSchemeFile);
            dataSetT.ReadXml(populateDataSet == null ? xmlSchemeFile : xmlDataFile, XmlReadMode.IgnoreSchema);

            var autoSource = new BindingSource();
            var repairSource = new BindingSource();

            autoSource.DataSource = dataSetT;
            autoSource.DataMember = "Auto";
            repairSource.DataSource = autoSource;
            repairSource.DataMember = "VIN_FK";

            dataGridView1.AutoGenerateColumns = true;
            dataGridView2.AutoGenerateColumns = true;
            dataGridView1.DataSource = autoSource;
            dataGridView2.DataSource = repairSource;
            dataSetT.AcceptChanges();

With AcceptChanges method now dataSetT.HasChanges() returns a realistic value. And then I use DataAdapter Update method.

Case closed.

Was it helpful?

Solution

I have found answer to my question, description is in edited question. Thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top