我的XAML文件:
<Grid>
<DataGrid x:Name="LibraryView" ItemsSource="{Binding Path=Elements[track]}" SelectionChanged="LoadAlbumDetails" SelectionMode="Single">
<DataGrid.Columns>
<DataGridTextColumn Header="Artist" Binding="{Binding Path=Element[artist_name].Value}" />
<DataGridTextColumn Header="Album" Binding="{Binding Path=Element[album_name].Value}"/>
<DataGridTextColumn Header="Length" Binding="{Binding Path=Element[duration].Value}"/>
<DataGridTextColumn Header="Price" Binding="{Binding Path=Element[price].Value}"/>
</DataGrid.Columns>
</DataGrid>
我的XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<albums>
<track>
<id>211</id>
<name>If you say something..</name>
<duration>156</duration>
<artist_id>13</artist_id>
<artist_name>Richard Keating</artist_name>
<album_id>29</album_id>
<album_name>Don't say anything..</album_name>
<price>$10</price>
</track>
<track>
<id>212</id>
<name>My heart is a stereo</name>
<duration>150</duration>
<artist_id>14</artist_id>
<artist_name>Maroon 5</artist_name>
<album_id>30</album_id>
<album_name>Maroon 5 stereo</album_name>
<price>$15</price>
</track>
</albums>
在我的.xaml.cs
中//初始加载XML以绑定到datagrid
protected void LoadAlbumDetails(object sender, SelectionChangedEventArgs e)
{
IList rows = LibraryView.SelectedItems;
XElement row = (XElement)rows[0];
//MessageBox.Show(row.Element("album_name").Value.ToString());
}
//Search button
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
//Am trying to use the concept of DataSet.Select or DataTable.Select
}
当我在文本框中输入Maroon 5并单击搜索按钮时,我想使用类似
的内容DataSet.Select("columnName1 like 'Maroon 5'");
然后重新绑定DataGrid。
答案 0 :(得分:1)
您可以使用CollectionView过滤数据网格,教程为here。
private CollectionView _collectionView;
public IList Tracks { get; set; } // your itemssource
public string FilterString { get; set; } // bind to your search textbox
private void Init() // call this when you first init your datagrid
{
_collectionView = CollectionViewSource.GetDefaultView(Tracks);
_collectionView.Filter = TrackFilter;
}
private bool TrackFilter(object item)
{
return track.columnName1.Contains( _filterString );
}
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
_collectionView.Refresh();
}