Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

C# WPF DataTable into a CollectionViewSource use that for a ViewList

I'm have gotten data from a database, now I want to put into a CollectionViewSource and then use that as the source for a ListView. I want to do this so I can filter the ListView. However I don't understand how pass the Datatable from c# to the XAML CollectionViewSource?

<Window.Resources>
    <ResourceDictionary >
        <CollectionViewSource x:Key="NamesOfCompany"/>
    </ResourceDictionary>
</Window.Resources>

<ListView x:Name="listView" ...
          ItemsSource="{Binding Source={StaticResource NamesOfCompany}}" >
    <ListView.View>
        <GridView>
            <GridViewColumn Header="CompanyName" 
                DisplayMemberBinding="{Binding CompanyName}" />
        </GridView>
   </ListView.View>
</ListView>

<Grid Margin="2,441.5,-2,0" Grid.Row="1" >
   <TextBox x:Name="txt_search" Height="22" TextWrapping="Wrap" Text="{Binding TextSearch,UpdateSourceTrigger=PropertyChanged}" Width="234" Margin="0,0,451,8" TextChanged="txt_search_TextChanged" HorizontalAlignment="Right" VerticalAlignment="Bottom" VerticalContentAlignment="Center"/>
</Grid>

All my data from my database is held in

private DataTable dt;

I'm not sure how you assign all the data from dt into the CollectionViewSource.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need a collectionviewsource to bind to datatable. Simply connect the datatable directly to the ItemsSource. One way to do this is in the code behind...

DataView dv = new DataView();
dv.Table = dt;
dv.Filter = "CompanyName='abc'";
listView.ItemsSource = dv;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...