Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

DataContext not working after clearing Datatable .

$
0
0

I have been trying to use Datatable as DataContext for my XAML UI which has various textbox controls with Binding to various fields of DataTable.

My code snippet while intial loading look like this:

string sqlStmt = "select * from Vendors.Vendor where 1 = 0";
            daVendor = new SqlDataAdapter(sqlStmt, BPMUtilitiesCore.ActiveSQLConnection);
            daVendor.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            SqlCommandBuilder cmdbldr = new SqlCommandBuilder(daVendor);
            daVendor.Fill(dsVendor,"Vendor");
            dsVendor.Tables["Vendor"].Columns["Company"].DefaultValue = string.Empty;
            DataRow dataRow = dsVendor.Tables["Vendor"].NewRow();
            dataRow["VendorID"] = Convert.ToInt32(GetScalar("select IDENT_Current('Vendors.Vendor')")) + 1;
            dsVendor.Tables["Vendor"].Rows.Add(dataRow);
            this.DataContext = dsVendor.Tables["Vendor"];

The above code works perfect for me. But now issue is after saving the DataRow or vendor record , I used to clear the DataTable and adds a new row/record like above with default values and set the Datatable again as DataContext for UI with below code snippet:

if (!dsVendor.HasErrors)
            {
                if (dsVendor.HasChanges())
                {
                    if (daVendor.Update(dsVendor, "Vendor") > 0)
                    {
                        MessageBox.Show("Vendor saved successfully", "My App", MessageBoxButton.OK, MessageBoxImage.Information);
                        this.dsVendor.Tables["Vendor"].Clear();
                        if (this.dsVendor.Tables["Vendor"].Rows.Count == 0)
                        {
                            DataRow dataRow = dsVendor.Tables["Vendor"].NewRow();
                            dsVendor.Tables["Vendor"].Rows.Add(dataRow);
                            this.DataContext = null;
                            this.DataContext = dsVendor.Tables["Vendor"];
                        }
                    }
                }
            }

Now my DataBindings are not getting updated even it seems they are breaking not showing any information. I checked both the datatable and  datacontext , they have correct data.

Please let me know what I am missing or how can I overcome this issue asap..

Thanks in advance.


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>