根据搜索条件自动绑定DataGrid
我的 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
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。
My XAML file:
<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>
My XML file:
<?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>
in my .xaml.cs
//For initial loading for the XML to be bound to the 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
}
When i type Maroon 5 in text box and click search button , i want to use something like
DataSet.Select("columnName1 like 'Maroon 5'");
and then rebind the DataGrid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 CollectionView 过滤数据网格,教程位于此处。
You could just filter the datagrid using CollectionView, a tutorial is here.