在DATAGRID中合并内容相同的单元格及实现行色彩交替

 

DATAGRID中合并内容相同的单元格及实现行色彩交替

经常在网上可以看到很多在DATAGRID中合并单元格的文章,感觉蛮不错的!
可是,我发现他不能实现按合并后的单元格实现“行”色彩交替
所以,就花了一些时间,研究了一下,终于能够在DATAGRID中实现合并单元格后
又能让其实现另类的行色彩交替。

Sub
SpanGrid()

        Dim i As Integer

        Dim j As Integer

        Dim intSpan As Integer

        Dim strTemp As String

        Dim flag As Boolean = True

        For i = 0 To DataGrid1.Items.Count - 1

            intSpan = 1

             strTemp = DataGrid1.Items(i).Cells(3).Text

            If flag = True Then

                DataGrid1.Items(i).BackColor = Color.LemonChiffon

                For j = i + 1 To DataGrid1.Items.Count - 1

                    If String.Compare(strTemp, DataGrid1.Items(j).Cells(3).Text) = 0 Then

                        intSpan += 1

                         DataGrid1.Items(i).Cells(3).RowSpan = intSpan

                        DataGrid1.Items(j).Cells(3).Visible = False

                         DataGrid1.Items(j).BackColor = Color.LemonChiffon

                    Else

                        Exit For

                    End If

                    flag = False

                Next

                i = j - 1

            Else

                DataGrid1.Items(i).BackColor = Color.LightYellow

                For j = i + 1 To DataGrid1.Items.Count - 1

                    If String.Compare(strTemp, DataGrid1.Items(j).Cells(3).Text) = 0 Then

                        intSpan += 1

                        DataGrid1.Items(i).Cells(3).RowSpan = intSpan

                        DataGrid1.Items(j).Cells(3).Visible = False

                         DataGrid1.Items(j).BackColor = Color.LightYellow

                    Else

                        Exit For

                    End If

                    flag = True

                Next

                i = j - 1

            End If

        Next

 

    End Sub

转载于:https://www.cnblogs.com/zhongru_tu/archive/2005/01/27/98302.html

  • 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、付费专栏及课程。

余额充值