【问题标题】:DataGridView XElement filtering via Textbox textDataGridView XElement 通过文本框文本过滤
【发布时间】:2013-01-31 11:33:03
【问题描述】:

这是我的“项目”:

我要做的是将外部 .xml 文件中的数据加载到 DataGrid 中:

XElement Persons = XElement.Load(@"d:\persons.xml");
dataGrid1.DataContext = Persons;

它工作得很好,但这是一个我无法弄清楚的问题:在 DataGrid 的顶部,您可以看到 TextBox,所以我需要使用 textBox1.text 作为 DataGrid 的数据过滤器。假设如果用户输入字母“a”,在 DataGriw 中我们应该只看到 2 行,名称为 pAul,行名称为国家 russiA,因为这两行的数据中都包含字母“a”。如果您还可以帮助我包含和排除某些列的搜索能力,那就太好了。最后,如果用户点击他使用搜索按钮找到的行 - 来自销售的数据应放置在右侧的标签中。我也很高兴知道如何加载未在 DataGrid 中实际显示的所选行的数据。假设在 xml 文件中我们有关于薪水的数据,但我们没有在 DataGrid 中显示它,我们需要在用户进行行选择时将其加载到第 4 个标签。这是我的 XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="679" Width="1137">
<Grid Height="645" Name="grid1" Width="1119">
    <DataGrid Height="300" ItemsSource="{Binding Path=Elements[person]}" Margin="26,42,839,297" Name="dataGrid1" Width="250">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Element[name].Value}" Header="Name" />
            <DataGridTextColumn Binding="{Binding Path=Element[country].Value}" Header="Country" />
            <DataGridTextColumn Binding="{Binding Path=Element[age].Value}" Header="Age" />
        </DataGrid.Columns>
    </DataGrid>
    <TextBox Height="24" HorizontalAlignment="Left" Margin="26,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="250" />
    <Label Content="Name goes here on row select" Height="28" HorizontalAlignment="Left" Margin="307,28,0,0" Name="label1" VerticalAlignment="Top" Width="188" />
    <Label Content="Country goes here on row select" Height="28" HorizontalAlignment="Left" Margin="307,72,0,0" Name="label2" VerticalAlignment="Top" Width="188" />
    <Label Content="Age goes here on row select" Height="28" HorizontalAlignment="Left" Margin="307,120,0,0" Name="label3" VerticalAlignment="Top" Width="188" />
</Grid>

请像绝对的初学者一样跟我说话,因为当你使用一些特别聪明的东西时,我很难理解。一步一步受到高度赞赏。谢谢...

【问题讨论】:

  • 不清楚你在问什么。您是否在询问有关使用 LINQ to XML 搜索 XML 的问题?或者您是在询问一些更新 UI 的代码?
  • 我在问如何使用文本框过滤数据网格中的结果。在 Windows 窗体中,我使用了数据集过滤器,但在这里我不知道该怎么做。

标签: c# wpf xml linq datagridview


【解决方案1】:

要过滤 Datagrid,您需要创建一个 CollectionViewSource 并将其分配给 DataGrid 的 ItemsSource:

private void Load()
{        
        XElement Persons = XElement.Load(@"d:\persons.xml");

        System.ComponentModel.ICollectionView c = System.Windows.Data.CollectionViewSource.GetDefaultView(Persons.Elements());
        c.Filter = new Predicate<object>(CollectionViewSource_Filter);

        dataGrid1.ItemsSource = c;
}

private Boolean CollectionViewSource_Filter(object i)
{
        return (i as XElement).Element("name").Value.ToString.Contains(textBox1.Text);
}

要在过滤器更改时刷新 DataGrid,需要此方法:c.Refresh();

如果过滤器不区分大小写,请查看Case insensitive 'Contains(string)'

显示所选人员的姓名:

<TextBlock Text="{Binding Path=SelectedItem.Element[name].Value, ElementName=dataGrid1}"></TextBlock>

【讨论】:

  • 感谢您的回复。但是这段代码给我返回了几个错误,它们是:错误1“System.Windows.Data.CollectionViewSource.GetDefaultView(对象)”的最佳重载方法有一些无效参数错误2参数“1”:“组方法的类型转换” ” 在“对象”中不允许出现错误 3“对象 WpfApplication1.MainWindow.CollectionViewSource_Filter (System.Xml.Linq.XElement)”的返回类型错误
  • 错误 4 无法在“System.Predicate ”中隐式转换类型“System.Predicate ”。存在显式转换(由于缺少强制转换)错误 5“string.ToString (System.IFormatProvider)”是一种“方法”,在此上下文中是不允许的
  • 由于数据绑定,它非常简单。您可以将“姓名”替换为 xml 文件中的所有其他属性,甚至是“薪水”。
  • 要更新代码中的标签,每次DataGrid的SelectionChanged事件发生时都必须调用label1.Content = dataGrid1.SelectedItem.Element("name").Value。为了获得更好的性能,您可以将 System.ComponentModel.ICollectionView 对象放在方法 Load 之外,然后在文本更改时仅调用 c.Refresh()
  • 由于属性SelectedItem是object类型,需要先将其转为XElement类型,并检查是否不为null,否则会出现NullReferenceException:if (dataGrid1.SelectedItem != null) label1.Content = (dataGrid1.SelectedItem as XElement).Element("name").Value;跨度>
猜你喜欢
  • 2012-07-25
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-25
  • 1970-01-01
相关资源
最近更新 更多