I'm trying to convert MSDN's video on "How Do I: Create a Simple Data Entry Form in WPF?" in VB to C#. I'm stuck at 20:11.
I started by creating a Typed DataSet and a CollectionView:
private CustomerDataSet CustomerData = new CustomerDataSet(); private CustomerDataSetTableAdapters.CustomersTableAdapter taCust = new CustomerDataSetTableAdapters.CustomersTableAdapter(); private CustomerDataSetTableAdapters.TableAdapterManager taManager = new CustomerDataSetTableAdapters.TableAdapterManager(); private CollectionView View; private void Window_Loaded(object sender, RoutedEventArgs e) { this.taCust.Fill(this.CustomerData.Customers); this.DataContext = CustomerData.Customers; this.View = (CollectionView)CollectionViewSource.GetDefaultView(this.CustomerData.Customers); }
I wired navigation buttons fine. My trouble began when I wired the Delete button's event. According to MSDN's CollectionView.CurrentItem documentation, I should be able to convert the CurrentItem to another object. I want to convert CurrentItem to a DataRow (or a Typed DataRow) so I can call the DataRow's Delete method.
private void btnDelete_Click(object sender, RoutedEventArgs e) { if (this.View.CurrentPosition != -1) { // This doesn't work, throws null exception: // DataRow row = this.View.CurrentItem as DataRow; // // This doesn't work either, throws null exception: // CustomerDataSet.CustomersRow row = this.View.CurrentItem as CustomerDataSet.CustomersRow; MessageBox.Show("row:" + row.ToString()); } }
Where am I going wrong? Thanks for any help!