Hi
I'm new to WPF can anyone explain me how to select a DataGrid Table Row and delete it in a button click using WPFin MVVM modal.
I can remove the row by button click, only by hard coding the value.
HostSystemInformation info = (from sysinfo in systemInformation
where sysinfo.Sno == 4
select sysinfo).First();
From above code i can delete only 4th row. I want solution to GET VALUE IN A VARIABLE WHEN I SELECT A ROW in datagrid table. I want to use that variable instead of hardcoded value 4. This coding not done in code-behind, done in seperate ModalView file
I have copied my code below someone give a solution for this.
XAML:
<Button Content="Remove" Command="{Binding DeleteIp}" Grid.Row="0" Grid.Column="1" FontFamily="Ebrima" FontSize="12" Width="61" Height="25" HorizontalAlignment="right" VerticalAlignment="center"/>
<DataGrid Name="datagridIpTable" ItemsSource="{Binding SystemInformation}" SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}" AutoGenerateColumns="false" Grid.Row="1" Grid.Column="1" CanUserAddRows="False" >
<DataGrid.Columns >
<DataGridTextColumn Binding="{Binding Sno}" Header="S.No" MinWidth="50" />
<DataGridTextColumn Binding="{Binding strIpAddr}" Header="System Name" MinWidth="240"/>
<DataGridTextColumn Binding="{Binding strSystemName}" Header="IP Address" MinWidth="240"/>
<DataGridTextColumn Binding="{Binding strStatus}" Header="Status" MinWidth="140" />
</DataGrid.Columns>
</DataGrid>
rivate DelegateCommand deleteIp;
public DelegateCommand DeleteIp
{
get { return deleteIp; }
set { deleteIp = value; }
}
private ObservableCollection<HostSystemInformation> systemInformation;
public ObservableCollection<HostSystemInformation> SystemInformation
{
get { return systemInformation; }
set { SetProperty(ref systemInformation, value); }
}
public UserBase_ViewModal()
{
SystemInformation = new ObservableCollection<HostSystemInformation>();
deleteIp = new DelegateCommand(DeleteSystemInformationInIpTable);
}
private void DeleteSystemInformationInIpTable()
{
try
{
if(systemInformation.Count>0)
{
int count=0;
foreach (object eno in systemInformation)
{
HostSystemInformation info = (from sysinfo in systemInformation
where sysinfo.Sno == 4
select sysinfo).First();
systemInformation.Remove(info);
count++;
}
}
}
catch (Exception ex)
{
}
}
public class HostSystemInformation
{
public int Sno { get; set; }
public string strIpAddr { get; set; }
public string strSystemName { get; set; }
public string strStatus { get; set; }
}
Thanks in Advance...
Regards
R.Karthik