如何选择整个DataGrid表行并使用WPF单击按钮将其删除 [英] How to select a entire DataGrid Table Row and delete it in a button click using WPF

查看:113
本文介绍了如何选择整个DataGrid表行并使用WPF单击按钮将其删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是WPF的新手,任何人都可以解释我如何选择一个DataGrid表行并使用WPFin MVVM模式在按钮单击中删除它。





我可以通过点击按钮来删除行,只需通过对值进行硬编码。



 HostSystemInformation info =( from  sysinfo  in  systemInformation 
其中​​ sysinfo.Sno == 4
选择 sysinfo )。第一();





从上面的代码我只能删除第4行。当我在datagrid表中选择一个行时,我想要在变量中获得值的解决方案。我想使用那个变量而不是硬编码的值4.这个编码没有在代码隐藏中完成,在单独的ModalView文件中完成



我已将我的代码复制到某人下面解决方案。



XAML:

< Button Content =  删除 Command =   {Binding DeleteIp} Grid.Row =   0 Grid.Column =   1 FontFamily =   Ebrima FontSize =   12宽度= < span class =code-string>  61高度=   25 Horizo​​ntalAlignment =   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}标题= S.No MinWidth = 50 />
< DataGridTextColumn Binding = {Binding strIpAddr} Header = 系统名称 MinWidth = 240 />
< DataGridTextColumn Binding = {Binding strSystemName} Header = IP地址 MinWidth = 240 />
< DataGridTextColumn Binding = {Binding strStatus} Header = 状态 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,); }
}

public UserBase_ViewModal()
{
SystemInformation = new ObservableCollection< HostSystemInformation>();
deleteIp = new DelegateCommand(DeleteSystemInformationInIpTable);
}

private void DeleteSystemInformationInIpTable()
{
尝试
{
if (systemInformation.Count> 0 )
{
int count = 0 ;
foreach object eno in systemInformation)
{
HostSystemInformation info =( from sysinfo in systemInformation
其中 sysinfo.Sno == 4
选择 sysinfo).First(); / * 这里不是第4行我需要按任意行动态传递变量* /
systemInformation.Remove(info);
count ++;
}
}
}
catch (例外情况)
{
// 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 ; }
}









在此先感谢.. 。





问候

R.Karthik

解决方案

请看这个链接..



http://www.aspsnippets.com/Articles/Selecting-GridView-Row-by-clicking-anywhere-on-the-Row.aspx [ ^ ]

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(); /*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

解决方案

Please see this link..

http://www.aspsnippets.com/Articles/Selecting-GridView-Row-by-clicking-anywhere-on-the-Row.aspx[^]


这篇关于如何选择整个DataGrid表行并使用WPF单击按钮将其删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆