private void purchaseDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { var test = (sender as DataGrid).SelectedItem; ObservableCollection <object> allPurchaseDetail = new ObservableCollection <object>(); int? id = (int?)test?.GetType().GetProperty("Purchase_ID")?.GetValue(test, null); string tel = test?.GetType().GetProperty("Tel")?.GetValue(test, null).ToString(); getCustomer(tel); #region PurchaseDetail DataGridDetailPurchase.Columns.Clear(); DataGridDetailPurchase.AutoGenerateColumns = true; if (id != null) { int index = (int)id; var dt = QueryForSQLServer.GetPurchaseDetail(index); foreach (DataRow item in dt.Rows) { var _p = new { Name = item["Name"], //Price = item["Price"], Quantity = item["Quantity"], Total = item["Total"] }; allPurchaseDetail.Add(_p); } DataGridDetailPurchase.ItemsSource = allPurchaseDetail; } else { purchaseDataGrid.SelectedIndex = -1; allPurchaseDetail.Clear(); DataGridDetailPurchase.ItemsSource = allPurchaseDetail; } #endregion btnAddItem.Visibility = Visibility.Collapsed; btnDeleteItem.Visibility = Visibility.Collapsed; btnUpdate.Visibility = Visibility.Collapsed; btnDone.Visibility = Visibility.Collapsed; } catch (Exception ex) { Debug.WriteLine(ex.Message.ToString()); } }
private void editPurchase_MenuItem_Click(object sender, RoutedEventArgs e) { btnAddItem.Visibility = Visibility.Visible; btnDeleteItem.Visibility = Visibility.Visible; btnUpdate.Visibility = Visibility.Visible; btnDone.Visibility = Visibility.Visible; var Selecteditem = (sender as FrameworkElement).DataContext; current_id = (int)Selecteditem?.GetType().GetProperty("Purchase_ID")?.GetValue(Selecteditem, null); DataGridDetailPurchase.AutoGenerateColumns = false; var dt = QueryForSQLServer.GetPurchaseDetail(current_id); DataGridDetailPurchase.Columns.Clear(); //customer string tel = Selecteditem?.GetType().GetProperty("Tel")?.GetValue(Selecteditem, null).ToString(); getCustomer(tel, false); //DataGridDetailPurchase DataGridDetailPurchase.Columns.Add(new DataGridTextColumn() { Header = dt.Columns[1].ColumnName, Binding = new Binding { Path = new PropertyPath("Product_Name") } }); DataGridDetailPurchase.Columns.Add(new DataGridTextColumn() { Header = dt.Columns[2].ColumnName, Binding = new Binding { Path = new PropertyPath("Quantity"), Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged } }); DataGridDetailPurchase.Columns[1].IsReadOnly = false; DataGridDetailPurchase.Columns.Add(new DataGridTextColumn() { Header = dt.Columns[4].ColumnName, Binding = new Binding { Path = new PropertyPath("SubTotal") } }); collection = new ObservableCollection <object>(); foreach (DataRow row in dt.Rows) { //collection.Add(row.ItemArray); collection.Add(new { Product_ID = row.ItemArray[0], Product_Name = row.ItemArray[1], Quantity = row.ItemArray[2], Unit_Price = row.ItemArray[3], SubTotal = row.ItemArray[4], PurchaseDetail_ID = row.ItemArray[5] }); } DataGridDetailPurchase.ItemsSource = collection; }