Haime Reyes
Haime Reyes

Reputation: 87

c# wpf Dynamic DataGrid

I have a datagrid1 that has 4 column(Balance, Amount, Transaction, Date). Another datagrid2 consist of names. When I click in the names, the datagrid1 changes its values base on the "name" profile in my database. In the datagrid2 I put "Show All" row where whenever I select the "Show All", the datagrid1 should add another column in the most left and name the column "Name" then the total column will be 5. And whenever "Show All" is not selected the column of datagrid1 should return to 4. Now how can I do that? One solution in my mind is that create 2 exact datagrid, same position, the just hide/show it whenever I select "Show All" or select names but I think its wrong if there's other way that is more efficient. I want to do this because you know when I select "Show All" and look in my datagrid1, I can't tell which transaction is this because there is no name included. Thanks in advance.

datagrid XAML code:

 <DataGrid x:Name="accountsBalance_grd"
                  HorizontalAlignment="Left"
                  Margin="266,118,0,0"
                  VerticalAlignment="Top"
                  Height="498"
                  Width="836"
                  AreRowDetailsFrozen="True"
                  AutoGenerateColumns="False"
                  SelectionChanged="accountsBalance_grd_SelectionChanged"
                  CanUserReorderColumns="False"
                  CanUserResizeColumns="False"
                  CanUserResizeRows="False"
                  CanUserSortColumns="False"
                  CanUserAddRows="False"
                  BorderBrush="Blue"
                  Background="White"
                  Foreground="Blue"
                  BorderThickness="2"
                  HorizontalGridLinesBrush="Black">
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="FontSize"
                            Value="20" />
                </Style>
            </DataGrid.ColumnHeaderStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Total Balance"
                                    Width="150"
                                    Binding="{Binding accountBalanceTotal}"
                                    IsReadOnly="True" />
                <DataGridTextColumn Header="Amount"
                                    Width="120"
                                    Binding="{Binding accountBalanceAmount}"
                                    IsReadOnly="True" />
                <DataGridTextColumn Header="Transaction"
                                    Width="160"
                                    Binding="{Binding accountBalanceTransaction}"
                                    IsReadOnly="True" />
                <DataGridTextColumn Header="Date"
                                    Width="*"
                                    Binding="{Binding accountBalanceDate}"
                                    IsReadOnly="True" />
            </DataGrid.Columns>
            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="FontSize"
                            Value="15" />
                    <Setter Property="FontFamily"
                            Value="Arial" />
                    <Setter Property="FontWeight"
                            Value="Bold" />
                    <Setter Property="Foreground"
                            Value="Black" />
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>

Upvotes: 0

Views: 81

Answers (1)

Naresh Ravlani
Naresh Ravlani

Reputation: 1620

Don't use two grids !! You can take advantage of WPF bindings to come over this issue. You can show\hide "Name" column on the "Select All" option (are you using a checkbox for it?). To understand how to show\hide Datagrid column refer this link

Your second concern is about selecting a different name and loading data in datagrid1, for that you can Set Datasource of Datagrid1 whenever "SelectedName" of datagrid2 changes. Again, that's WPF bindings only.

Your scenario is pretty straight forward to be dealt by WPF. Let me know if you need more details on this.

Upvotes: 1

Related Questions