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.