Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <DataGrid HorizontalAlignment="Left" Margin="10,21,0,0" VerticalAlignment="Top" RenderTransformOrigin="-18.493,-6.121" Height="288" Width="497"
- ItemsSource="{Binding ProductList, Mode=TwoWay}"
- AutoGenerateColumns="False"
- CanUserAddRows="True"
- CanUserDeleteRows="True">
- <DataGrid.Columns>
- <DataGridTextColumn Header="Product Name" Width="*" Binding="{Binding ProductName, Mode=TwoWay}"/>
- <DataGridTextColumn Header="Description" Width="*" Binding="{Binding Descriptiopn, Mode=TwoWay}"/>
- <DataGridTextColumn Header="Price" Width="*" Binding="{Binding Price, Mode=TwoWay}"/>
- </DataGrid.Columns>
- <i:Interaction.Triggers>
- <i:EventTrigger EventName="RowEditEnding">
- <Custom:EventToCommand PassEventArgsToCommand="True" Command="{Binding EditrowEvent}"/>
- </i:EventTrigger>
- </i:Interaction.Triggers>
- </DataGrid>
- public MainViewModel()
- {
- if (!IsInDesignMode)
- {
- ProductList = new ObservableCollection<EntryViewModel>();
- dbContext = new DataProvider();
- Thread UpdateGui = new Thread(UpdateteGui);
- UpdateGui.Start();
- EditrowEvent = new RelayCommand<DataGridRowEditEndingEventArgs>(RowEditing);
- }
- }
- private void RowEditing(DataGridRowEditEndingEventArgs e)
- {
- if(e.EditAction == DataGridEditAction.Commit)
- {
- EntryViewModel emv = e.Row.DataContext as EntryViewModel;
- Entry newEntry = new Entry
- {
- ProductName = emv.ProductName,
- Descriptiopn = emv.Descriptiopn,
- Price = emv.Price
- };
- dbContext.WriteToDB(newEntry);
- }
- }
- public class DataProvider
- {
- ModelContext dbContext;
- public List<Entry> Allproducts()
- {
- using(dbContext = new ModelContext())
- {
- List<Entry> _entrydata = new List<Entry>();
- var thdata = dbContext.Data;
- foreach (var item in thdata)
- {
- _entrydata.Add(new Entry { Descriptiopn = item.Desc, Price = item.Pprice, ProductName = item.ProdName });
- }
- return _entrydata;
- }
- }
- public bool WriteToDB(Entry entry)
- {
- try
- {
- dbContext.Data.Add(new Data { ProdName = entry.ProductName, Desc = entry.Descriptiopn, Pprice = entry.Price, Id = Guid.NewGuid() });
- dbContext.SaveChanges();
- }
- catch (Exception)
- {
- return false;
- }
- return true;
- }
- }
- System.Reflection.TargetInvocationException was unhandled
- _HResult=-2146232828
- _message=Exception has been thrown by the target of an invocation.
- HResult=-2146232828
- IsTransient=false
- Message=Exception has been thrown by the target of an invocation.
- Source=mscorlib
- StackTrace:
- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
- at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
- at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
- at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
- at GalaSoft.MvvmLight.Helpers.WeakAction`1.Execute(T parameter) in c:MvvmLightSourceGalaSoft.MvvmLightGalaSoft.MvvmLight (PCL)HelpersWeakActionGeneric.cs:line 204
- at GalaSoft.MvvmLight.CommandWpf.RelayCommand`1.Execute(Object parameter) in c:MvvmLightSourceGalaSoft.MvvmLightGalaSoft.MvvmLight (PCL)CommandRelayCommandGeneric.cs:line 232
- at GalaSoft.MvvmLight.Command.EventToCommand.Invoke(Object parameter) in c:MvvmLightSourceGalaSoft.MvvmLightGalaSoft.MvvmLight.Platform (NET45)CommandEventToCommand.cs:line 338
- at System.Windows.Interactivity.TriggerBase.InvokeActions(Object parameter)
- at System.Windows.Interactivity.EventTriggerBase.OnEvent(EventArgs eventArgs)
- at System.Windows.Interactivity.EventTriggerBase.OnEventImpl(Object sender, EventArgs eventArgs)
- at System.Windows.Controls.DataGrid.OnRowEditEnding(DataGridRowEditEndingEventArgs e)
- at System.Windows.Controls.DataGrid.OnExecutedCommitEdit(ExecutedRoutedEventArgs e)
- at System.Windows.Controls.DataGrid.OnExecutedCommitEdit(Object sender, ExecutedRoutedEventArgs e)
- at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
- at System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding)
- at System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
- at System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
- at System.Windows.UIElement.OnExecutedThunk(Object sender, ExecutedRoutedEventArgs e)
- at System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(Delegate genericHandler, Object target)
- at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
- at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
- at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
- at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
- at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
- at System.Windows.Input.RoutedCommand.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated)
- at System.Windows.Input.RoutedCommand.Execute(Object parameter, IInputElement target)
- at System.Windows.Controls.DataGrid.EndEdit(RoutedCommand command, DataGridCell cellContainer, DataGridEditingUnit editingUnit, Boolean exitEditMode)
- at System.Windows.Controls.DataGrid.OnCurrentCellChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
- at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
- at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
- at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
- at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
- at System.Windows.DependencyObject.SetCurrentValueInternal(DependencyProperty dp, Object value)
- at System.Windows.Controls.DataGrid.set_CurrentCellContainer(DataGridCell value)
- at System.Windows.Controls.DataGrid.set_FocusedCell(DataGridCell value)
- at System.Windows.Controls.DataGridCell.OnAnyGotFocus(Object sender, RoutedEventArgs e)
- at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
- at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
- at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
- at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
- at System.Windows.FrameworkElement.OnGotFocus(RoutedEventArgs e)
- at System.Windows.UIElement.IsFocused_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
- at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
- at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
- at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
- at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
- at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
- at System.Windows.DependencyObject.SetValue(DependencyPropertyKey key, Object value)
- at System.Windows.Input.FocusManager.OnFocusedElementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
- at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
- at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
- at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
- at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
- at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
- at System.Windows.Input.FocusManager.SetFocusedElement(DependencyObject element, IInputElement value)
- at System.Windows.Input.KeyboardNavigation.UpdateFocusedElement(DependencyObject focusTarget)
- at System.Windows.FrameworkElement.OnGotKeyboardFocus(Object sender, KeyboardFocusChangedEventArgs e)
- at System.Windows.Input.KeyboardFocusChangedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
- at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
- at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
- at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
- at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
- at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
- at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
- at System.Windows.Input.InputManager.ProcessStagingArea()
- at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
- at System.Windows.Input.KeyboardDevice.ChangeFocus(DependencyObject focus, Int32 timestamp)
- at System.Windows.Input.KeyboardDevice.TryChangeFocus(DependencyObject newFocus, IKeyboardInputProvider keyboardInputProvider, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
- at System.Windows.Input.KeyboardDevice.Focus(DependencyObject focus, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
- at System.Windows.Input.KeyboardDevice.Focus(IInputElement element)
- at System.Windows.UIElement.Focus()
- at System.Windows.Input.KeyboardNavigation.Navigate(DependencyObject currentElement, TraversalRequest request, ModifierKeys modifierKeys, DependencyObject firstElement)
- at System.Windows.FrameworkElement.MoveFocus(TraversalRequest request)
- at System.Windows.Controls.DataGrid.OnTabKeyDown(KeyEventArgs e)
- at System.Windows.Controls.DataGrid.OnKeyDown(KeyEventArgs e)
- at System.Windows.UIElement.OnKeyDownThunk(Object sender, KeyEventArgs e)
- at System.Windows.Input.KeyEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
- at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
- at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
- at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
- at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
- at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
- at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
- at System.Windows.Input.InputManager.ProcessStagingArea()
- at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
- at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
- at System.Windows.Interop.HwndKeyboardInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawKeyboardActions actions, Int32 scanCode, Boolean isExtendedKey, Boolean isSystemKey, Int32 virtualKey)
- at System.Windows.Interop.HwndKeyboardInputProvider.ProcessKeyAction(MSG& msg, Boolean& handled)
- at System.Windows.Interop.HwndSource.CriticalTranslateAccelerator(MSG& msg, ModifierKeys modifiers)
- at System.Windows.Interop.HwndSource.OnPreprocessMessage(Object param)
- at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
- at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
- at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
- at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
- at System.Windows.Interop.HwndSource.OnPreprocessMessageThunk(MSG& msg, Boolean& handled)
- at System.Windows.Interop.HwndSource.WeakEventPreprocessMessage.OnPreprocessMessage(MSG& msg, Boolean& handled)
- at System.Windows.Interop.ComponentDispatcherThread.RaiseThreadMessage(MSG& msg)
- at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
- at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
- at System.Windows.Threading.Dispatcher.Run()
- at System.Windows.Application.RunDispatcher(Object ignore)
- at System.Windows.Application.RunInternal(Window window)
- at System.Windows.Application.Run(Window window)
- at System.Windows.Application.Run()
- at WPFClient.App.Main() in c:UsersVavyDocumentsVisual Studio 2013ProjectsWPFMSMQDatagridToDataBaseWPFClientobjDebugApp.g.cs:line 0
- at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
- at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
- at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
- at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
- at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
- at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
- at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
- at System.Threading.ThreadHelper.ThreadStart()
- InnerException: System.NullReferenceException
- _HResult=-2147467261
- _message=Object reference not set to an instance of an object.
- HResult=-2147467261
- IsTransient=false
- Message=Object reference not set to an instance of an object.
- Source=WPFClient
- StackTrace:
- at WPFClient.ViewModel.MainViewModel.RowEditing(DataGridRowEditEndingEventArgs e) in c:UsersVavyDocumentsVisual Studio 2013ProjectsWPFMSMQDatagridToDataBaseWPFClientViewModelMainViewModel.cs:line 60
- InnerException:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement