text: Q:
  
  Remove selected row from DataGrid?
  
  Hi guys i had been researching for two days about how to delete a selected row in wpf datagrid but i found nothing useful .I used PreviewKeyDown event handler below but all in vain.I just want to remove the row selected by the user.
  I am MVVM pattern and my code is :
    public ObservableCollection<VLANSPropertyClass> vlan { get; set; }
       vlan = new ObservableCollection<VLANSPropertyClass>();
        public ICommand AddVlan
          {
            get
            {
              if (_addVlan == null)
                _addVlan = new RelayCommand(() => this.AddVlans());
  
              return _addVlan;
            }
  
          }
  
       public ICommand RemoveVlan
          {
              get
              {
                  if (_removeVlan == null)
                      _removeVlan = new RelayCommand(() => this.RemoveVlans());
  
                  return _removeVlan;
              }
  
          }
     void AddVlans()
      {
            Console.WriteLine("Add vlan");
              var serial = new VLANSPropertyClass();
              serial.S_No = vlan.Count + 1;
              Console.WriteLine(serial.S_No);
              serial.vname = VlanName;
              Console.WriteLine(serial.vname);
              serial.vid = VlanID;
              Console.WriteLine(serial.vid);
              serial.ip = VlanIP1 + "." + VlanIP2 + "." + VlanIP3 + "." + VlanIP4;
              Console.WriteLine(serial.ip);
              serial.vports = SelectedVlanPort;
              vlan.Add(serial);
          }
       void RemoveVlans()
          {
              var rem = new VLANSPropertyClass();
              rem.S_No = vlan.Remove();
              rem.vname = vlan.Remove();
              rem.ip = vlan.Remove();
  
          }
  
  Can any one provide me the solution to this problem.Any help would be greatly appreciable.
  
  A:
  
  Generally you use the MVVM design pattern in WPF programs.  Your DataGrid control's ItemsSource property is bound to an ObservableCollection of objects.  To remove a row from the DataGrid, all you have to do is remove the object from the ObservableCollection.
  If you don't know what "MVVM" means, here's an MSDN article about the Model View View-Model Design Pattern.
  EDIT
  I am responding to your comment in here since the response requires more space than a comment will hold.
  You didn't include any XAML in your question, so I'm going to assume you've got something like this for your DataGrid definition:
  <DataGrid Name="MyDataGrid"
            ItemsSource="{Binding Path=vlan}"
            . . .>
      <DataGrid.Columns>
          . . .
      </DataGrid.Columns>
  </DataGrid>
  
  First, your view model needs a property of type VLANSPropertyClass to hold the item that's currently selected in the DataGrid:
  public VLANSPropertyClass SelectedVlan {
      get { return iSelectedVlan; }
      set { 
          iSelectedVlan = value;
          OnPropertyChanged ("SelectedVlan" );
      }
  }
  private VLANSPropertyClass iSelectedVlan = null;
  
  Next, you have to modify your XAML to bind the new property to the DataGrid's SelectedItem property:
  <DataGrid Name="MyDataGrid"
            ItemsSource="{Binding Path=vlan}"
            SelectedValue={Binding Path=SelectedVlan}"
            . . .>
      <DataGrid.Columns>
          . . .
      </DataGrid.Columns>
  </DataGrid>
  
  Then, your RemoveVlans method should look something like:
  void RemoveVlans() {
      if ( SelectedVlan != null ) {
          vlans.Remove( SelectedVlan );
          SelectedVlan = null;
      }
  
  That will do the trick.
  
  