如何选择整个DataGrid表行并使用WPF单击按钮将其删除 [英] How to select a entire DataGrid Table Row and delete it in a button click using 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 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}标题= 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屋!