Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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:
C#
<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>


C#
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(); /*Here instead of 4th row i need to pass variable dynamically by pressing any row */
                systemInformation.Remove(info);
                count++;
            }
        }
    }
    catch (Exception ex)
    {
        // MessageBox.Show(ex.Message);
    }
}

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
Posted
Updated 9-Nov-14 21:50pm
v4
Comments
Tomas Takac 9-Nov-14 8:59am    
You should try to solve the problem yourself first. If you have a specific problem, show us the code and explain where you are stuck.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900