将内部datagrid数据与外部datagrid wpf绑定

Posted

技术标签:

【中文标题】将内部datagrid数据与外部datagrid wpf绑定【英文标题】:Binding inner datagrid data with outter datagrid wpf 【发布时间】:2014-04-23 05:56:12 【问题描述】:

我有一个源数据网格,它绑定了一个名为 source 的数据表和一个内部数据网格(可以通过单击 rowdetails 中的按钮来查看),它绑定了一个名为 inner 的不同数据表。这两个属性都是从数据库中的 2 个不同表中填充的,但是“TradeID”是两个表之间的公共字段。当我最大化我的rowdetails时,我只想看到与那个“TraderID”相对应的行。这是我到目前为止的c#:

try
        
            SourceTable = new DataTable();
            InnerTable = new DataTable();

            SourceTable.Columns.AddRange(new DataColumn[]
                new DataColumn("InputID", typeof(int)),
                new DataColumn("TraderID", typeof(string)),
                new DataColumn("TradeDate", typeof(DateTime)),
                new DataColumn("TradeTime", typeof(TimeSpan)),
                new DataColumn("ClientName", typeof(string)),
                new DataColumn("CurPair", typeof(string)),
                new DataColumn("Amnt", typeof(int)),
                new DataColumn("Action", typeof(string)),
                new DataColumn("ExecutedRate", typeof(decimal))
            );

            InnerTable.Columns.AddRange(new DataColumn[]
                new DataColumn("TraderID", typeof(string)),
                new DataColumn("TradeDate", typeof(DateTime)),
                new DataColumn("TradeTime", typeof(TimeSpan)),
                new DataColumn("Amnt", typeof(int)),
                new DataColumn("Action", typeof(string)),
                new DataColumn("ExecutedRate", typeof(decimal)),
                new DataColumn("Type", typeof(string))
            );

            DataRow rowSource = null;
            DataRow rowInner = null;

            var OpenTradesQuery = from qa in connection.QuickAnalyzerInputs
                                  where qa.TradeClosedDateTime == null
                                  select new
                                  
                                      qa.InputID,
                                      qa.TraderID,
                                      qa.ClientTradedDate,
                                      qa.ClientTradedTime,
                                      qa.ClientName,
                                      qa.CurrencyPair,
                                      qa.TradedAmount,
                                      qa.Action,
                                      qa.ExecutedRate
                                  ;


            var ChildTradesQuery = from ct in connection.ChildTradeDetails
                                  join qa in connection.QuickAnalyzerInputs on ct.TradeID equals qa.InputID                                         
                                  where qa.TradeClosedDateTime == null
                                  select new
                                  
                                      ct.TraderID,
                                      ct.ClientTradedDate,
                                      ct.ClientTradedTime,
                                      ct.TradedAmount,
                                      ct.Action,
                                      ct.ExecutedRate,
                                      ct.Type
                                  ;

            if (OpenTradesQuery.Count() > 0)
            
                numOfrecords = OpenTradesQuery.Count();
                DataContext = this;

                foreach (var rowObj in OpenTradesQuery)
                
                    rowSource = SourceTable.NewRow();
                    SourceTable.Rows.Add(rowObj.InputID, rowObj.TraderID, rowObj.ClientTradedDate, rowObj.ClientTradedTime, rowObj.ClientName, rowObj.CurrencyPair, rowObj.TradedAmount, rowObj.Action, rowObj.ExecutedRate);
                

                foreach (var rowObj in ChildTradesQuery)
                
                    rowInner = SourceTable.NewRow();
                    InnerTable.Rows.Add(rowObj.TraderID, rowObj.ClientTradedDate, rowObj.ClientTradedTime, rowObj.TradedAmount, rowObj.Action, rowObj.ExecutedRate, rowObj.Type);
                

                this.DG_openTrades.RowDetailsVisibilityChanged += new EventHandler<ExtendedGrid.Microsoft.Windows.Controls.DataGridRowDetailsEventArgs>(dataGrid_RowDetailsVisibilityChanged);
            


private void dataGrid_RowDetailsVisibilityChanged(object sender, ExtendedGrid.Microsoft.Windows.Controls.DataGridRowDetailsEventArgs e)

        ExtendedGrid.Microsoft.Windows.Controls.DataGrid innerDataGrid = e.DetailsElement as ExtendedGrid.Microsoft.Windows.Controls.DataGrid;

        innerDataGrid.ItemsSource = ((IListSource)InnerTable).GetList();

一些 XAML:

<ExtendedGridControl:ExtendedDataGrid x:Name="DG_openTrades"
                                          RowHeaderWidth="17"  AutoGenerateColumns="False" HideColumnChooser="False" 
                                          ItemsSource="Binding SourceTable"
                                          IsSynchronizedWithCurrentItem="True"
                                          HorizontalScrollBarVisibility="Visible"
                                          VerticalScrollBarVisibility="Visible"
                                          SelectionMode="Extended" 
                                          SelectionUnit="FullRow"
                                          CanUserAddRows="False"
                                          CanUserDeleteRows="False"
                                          SearchAutoFilterVisibility="Visible" AutoFilterFontSize="10"
                                          Grid.ColumnSpan="3" Grid.Column="1" Grid.Row="2" Margin="0,0,0,1" 
                                          RowDetailsVisibilityMode="Collapsed"
                                          >
        <ExtendedGridControl:ExtendedDataGrid.RowDetailsTemplate>
            <DataTemplate>
                <ExtendedGridControl:ExtendedDataGrid Name="innerDataGrid" 
                                                      HeadersVisibility="Row" 
                                                      GroupByControlVisibility="Collapsed">                        
                </ExtendedGridControl:ExtendedDataGrid>
            </DataTemplate>
        </ExtendedGridControl:ExtendedDataGrid.RowDetailsTemplate>

        <Control:DataGrid.ContextMenu>
            <ContextMenu >
                <MenuItem Click="IncTradeAmnt_Click" Header="Increase">
                    <MenuItem.Icon>
                        <Image Width="15" Height="15" Source="../plus-26.png"/>
                    </MenuItem.Icon>
                </MenuItem>

                <MenuItem Click="DecTradeAmnt_Click" Header="Decrease">
                    <MenuItem.Icon>
                        <Image Width="15" Height="15" Source="../Images/minus-26.png"/>
                    </MenuItem.Icon>
                </MenuItem>

                <MenuItem Click="CloseTrade_Click" Header="Close">
                    <MenuItem.Icon>
                        <Image Width="15" Height="15" Source="../Images/close_window-26.png"/>
                    </MenuItem.Icon>
                </MenuItem>
            </ContextMenu>
        </Control:DataGrid.ContextMenu>

        <Control:DataGrid.Columns>

            <Control:DataGridTemplateColumn>
                <Control:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Click="ShowHideDetails" Foreground="Black">+</Button>
                    </DataTemplate>
                </Control:DataGridTemplateColumn.CellTemplate>
            </Control:DataGridTemplateColumn>

            <ExtendedColumn:ExtendedDataGridTextColumn Header="ID" AllowAutoFilter="True"
                                       Binding="Binding InputID" IsReadOnly="True" CanUserSort="True"/>

现在,当我最大化视图时,我只看到从 ChildTradeDetails 表中查询的所有内容,如何查询该数据表以仅显示与我选择的行中的 tradeId 对应的数据?

编辑

如何获取 RowDetailsVisibilityChanged 事件中所选行的第一列的值?

【问题讨论】:

【参考方案1】:

凭记忆告诉你,你可以像这样绑定选中的项目: SelectedItem="binding selectedRow, mode=twoway"

如果值可以更改,最好对observableCollection&lt;YourRowModelType&gt; 中的每一行使用模型,否则使用IEnumerable&lt;YourRowModelType&gt;

以此为例:

WPF Binding SelectedItem in DataGrid

然后您将能够在每个事件中访问选定的行...

这是一个 mvvm 模式...

希望对你有帮助

【讨论】:

以上是关于将内部datagrid数据与外部datagrid wpf绑定的主要内容,如果未能解决你的问题,请参考以下文章

从 Datagrid 到外部访问 Button 的 id?

wpf datagrid cell 设置焦点

datagrid与webAPI的数据交互(ef mvc )

拖放在 DataGrid (WPF) 中不起作用

将按钮列添加到 datagrid 视图导致问题

Flex 4.5:带有自定义 ItemRenderes 的 Spark DataGrid