合并datagrid中内容相同的单元格

有时,我们要把一列中内容相同的单元格合并起来。如下图:
此主题相关图片如下:



合并后的效果图:

此主题相关图片如下:



下面就说说怎么实现的:

Sub SpanGrid()
        Dim i As Integer
        Dim j As Integer
        Dim intSpan As Integer
        Dim strTemp As String
        For i = 0 To DGrid.Items.Count - 1
            intSpan = 1    

'得到第一列(颜色)、第一行单元格中的内容。这里得到是“红色Red”。(datagrid里用了模版列)
            strTemp = CType(DGrid.Items(i).Cells(0).Controls(1), System.Web.UI.WebControls.Label).Text

’循环判断。判断第一列中,和第一行相同的内容。相同做记号,intspan加一
            For j = i + 1 To DGrid.Items.Count - 1
                If String.Compare(strTemp, CType(DGrid.Items(j).Cells(1).Controls(1), System.Web.UI.WebControls.Label).Text) = 0 Then
                    intSpan += 1

'利用datagrid的rowspan属性。(设置控件中单元格跨越的行数为intspan)
                    DGrid.Items(i).Cells(0).RowSpan = intSpan

’把内容相同单元格隐藏
                    DGrid.Items(j).Cells(0).Visible = False
                Else
                    Exit For
                End If
            Next
            i = j - 1
        Next
    End Sub

=======

引用:

 Sub bindgrid()
        '把数据绑定到datagrid        
        ........
        SpanGrid()
End Sub
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 WPF 的 DataGrid 控件,要实现相同内容合并单元格,需要自定义一个合并单元格的行为,可以使用 Attached Behavior 或者 Behavior 实现。 以下是使用 Attached Behavior 的实现方法: 1. 创建一个名为 DataGridMergeCellBehavior 的类,并实现一个名为 MergeCells 的附加属性,用于启用合并单元格的行为。代码如下: ```csharp public static class DataGridMergeCellBehavior { public static bool GetMergeCells(DependencyObject obj) { return (bool)obj.GetValue(MergeCellsProperty); } public static void SetMergeCells(DependencyObject obj, bool value) { obj.SetValue(MergeCellsProperty, value); } public static readonly DependencyProperty MergeCellsProperty = DependencyProperty.RegisterAttached("MergeCells", typeof(bool), typeof(DataGridMergeCellBehavior), new PropertyMetadata(false, OnMergeCellsChanged)); private static void OnMergeCellsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (!(d is DataGrid dataGrid)) return; if ((bool)e.NewValue) dataGrid.LoadingRow += DataGrid_LoadingRow; else dataGrid.LoadingRow -= DataGrid_LoadingRow; } private static void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e) { var dataGrid = sender as DataGrid; if (dataGrid == null) return; // 获取当前行的数据项 var currentRowItem = e.Row.Item; // 获取当前行的所有单元格 var currentRowCells = e.Row.Descendants<DataGridCell>().ToList(); // 遍历当前行的所有单元格,将与下一行相同单元格合并 for (int i = 0; i < currentRowCells.Count; i++) { var currentCell = currentRowCells[i]; // 如果当前单元格已经被合并,则跳过 if (currentCell.IsMerged()) continue; var column = currentCell.Column; var binding = (column as DataGridBoundColumn)?.Binding as Binding; // 如果当前列没有绑定数据,则跳过 if (binding == null) continue; var path = binding.Path.Path; var currentCellValue = currentRowItem.GetType().GetProperty(path).GetValue(currentRowItem, null); // 查找下一行与当前行相同单元格 var nextRow = dataGrid.ItemContainerGenerator.ContainerFromIndex(e.Row.GetIndex() + 1) as DataGridRow; if (nextRow == null) break; var nextRowItem = nextRow.Item; var nextRowCells = nextRow.Descendants<DataGridCell>().ToList(); var nextCell = nextRowCells[i]; var nextCellValue = nextRowItem.GetType().GetProperty(path).GetValue(nextRowItem, null); // 如果下一行与当前行的单元格相同,则将它们合并 if (Equals(currentCellValue, nextCellValue)) currentCell.Merge(nextCell); } } } ``` 2. 在 DataGrid 控件应用 DataGridMergeCellBehavior 提供的 MergeCells 附加属性,启用合并单元格的行为。例如,以下代码将启用合并 DataGrid 的第一列单元格的行为: ```xml <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False" behaviors:DataGridMergeCellBehavior.MergeCells="True"> <DataGrid.Columns> <DataGridTextColumn Header="Column1" Binding="{Binding Column1}" /> <DataGridTextColumn Header="Column2" Binding="{Binding Column2}" /> <DataGridTextColumn Header="Column3" Binding="{Binding Column3}" /> </DataGrid.Columns> </DataGrid> ``` 在上面的例子DataGridMergeCellBehavior 提供的 MergeCells 附加属性被设置为 True,启用了合并单元格的行为。同时,第一列的单元格将会被合并,如果某个单元格的值与下一行相同,则它们会被合并成一个单元格
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值