C#.NET WPF-DataTable(SQLite)作为带有分组的DataGrid源

时间:2019-04-21 21:27:10

标签: c# wpf data-binding datatable datagrid

我是WPF的新手,无法以分组形式显示来自SQLite DB的数据。有人可以帮忙吗?

我的最新版本在编译后引发错误:object reference not set to an instance of an object

我不坚持使用数据表作为源,我只需要以分组方式显示来自SQL表的数据。最好的选择是合并两个查询,以便组详细信息来自查询1,数据行来自查询2。如在SQL中,表连接在特定列上。

下面的C#代码。

// Fired on Window_Loaded event
public void FillDataGridFromSQL()
{
        SQLiteConnection oSQLiteConnection = new SQLiteConnection("Data Source=rem_test_database.db");
        SQLiteCommand oCommand = oSQLiteConnection.CreateCommand();
        oCommand.CommandText = "SELECT * FROM first_table";
        m_oDataAdapter = new SQLiteDataAdapter(oCommand.CommandText, oSQLiteConnection);
        SQLiteCommandBuilder oCommandBuilder = new SQLiteCommandBuilder(m_oDataAdapter);

        m_oDataSet = new DataSet();
        m_oDataAdapter.Fill(m_oDataSet);
        m_oDataTable = m_oDataSet.Tables[0];

        CollectionView collectionView = new CollectionView(CollectionViewSource.GetDefaultView(m_oDataTable.DefaultView));
        // Line below produce exception!
        collectionView.GroupDescriptions.Add(new PropertyGroupDescription("id_nieruchomosci"));
        LeftDataGrid.ItemsSource = collectionView;
}

和XAML标记:

<!-- DataGrid -->
<DataGrid Grid.Column="0" Grid.Row="2" Name="LeftDataGrid" AutoGenerateColumns="False">
    <!-- Grouping style-->
    <DataGrid.GroupStyle>
        <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <StackPanel>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="{Binding id_nieruchomosci}" />
                                            <TextBlock Text="{Binding id_nieruchomosci, StringFormat=Count: {0}}" Margin="30,0,0,0" />
                                        </StackPanel>
                                        <ItemsPresenter />
                                    </StackPanel>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
        </GroupStyle>
    </DataGrid.GroupStyle>
    <!-- Columns Definitions -->
    <DataGrid.Columns>
            <DataGridTextColumn Header="id_nieruchomosci" Binding="{Binding id_nieruchomosci}" />
            <DataGridTextColumn Header="id" Binding="{Binding id}" />
            <DataGridTextColumn Header="frakcja" Binding="{Binding frakcja}">
            </DataGridTextColumn>
            <DataGridTemplateColumn Header="fld_results">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Button Content="X" Click="Button_Click"/>
                            <Button Content="E" Click="Button_Click"/>
                            <Button Content="M" Click="Button_Click"/>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

0 个答案:

没有答案
相关问题