根据搜索条件自动绑定DataGrid

发布于 2025-01-08 10:23:14 字数 2379 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2025-01-15 10:23:14

您可以使用 CollectionView 过滤数据网格,教程位于此处

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();
}

You could just filter the datagrid using CollectionView, a tutorial is 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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文