简体   繁体   English

c#wpf动态数据网格

[英]c# wpf Dynamic DataGrid

I have a datagrid1 that has 4 column(Balance, Amount, Transaction, Date). 我有一个datagrid1,它有4列(余额,金额,交易,日期)。 Another datagrid2 consist of names. 另一个datagrid2由名称组成。 When I click in the names, the datagrid1 changes its values base on the "name" profile in my database. 当我单击名称时,datagrid1会根据数据库中的“名称”配置文件更改其值。 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? 在datagrid2中,我放置了“ Show All”行,每当我选择“ Show All”时,datagrid1应该在最左侧添加另一列,并将其命名为“ Name”,然后总列将为5。 “未选中”,datagrid1列应返回到4。现在该怎么做? 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. 我想到的一个解决方案是,创建2个精确的数据网格,相同的位置,每当我选择“全部显示”或选择名称时都将其隐藏/显示,但是如果有其他更有效的方法,我认为它是错误的。 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. 我想要这样做是因为您知道当我选择“显示全部”并查看我的datagrid1时,因为其中没有名称,所以我无法知道这是哪个事务。 Thanks in advance. 提前致谢。

datagrid XAML code: datagrid XAML代码:

 <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>

Don't use two grids !! 不要使用两个网格! You can take advantage of WPF bindings to come over this issue. 您可以利用WPF绑定来解决此问题。 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 要了解如何显示\\隐藏Datagrid列,请参考此链接

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. 您的第二个担心是选择一个不同的名称并将数据加载到datagrid1中,因为只要datagrid2的“ SelectedName”发生更改,就可以设置Datagrid1的数据源。 Again, that's WPF bindings only. 同样,那只是WPF绑定。

Your scenario is pretty straight forward to be dealt by WPF. 您的情况非常简单,可以由WPF处理。 Let me know if you need more details on this. 让我知道您是否需要更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM