DataGrid用法:合并内容相同的单元格

/// <summary>
        
/// DataGrid中相同单元格的合并
        
/// </summary>

         public   void  SpanGrid()
        
{
            
int i;
            
int j;
            
int intSpan;
            
string strTemp;

            
for (i = 0; i < DataGrid1.Items.Count;)
            
{
                intSpan 
= 1;
                
                strTemp 
= DataGrid1.Items[i].Cells[0].Text;
                
                
//循环判断,判断第一列中,和第一行相同的内容
                for (j = i + 1; j < DataGrid1.Items.Count; j ++)
                
{
                    
if (string.Compare(strTemp, DataGrid1.Items[j].Cells[1].Text) == 0)
                    
{
                        intSpan 
+= 1;

                        
//利用datagrid的RowSpan属性 
                        DataGrid1.Items[i].Cells[0].RowSpan = intSpan;

                        
//把内容相同单元格隐藏
                        DataGrid1.Items[j].Cells[0].Visible = false;
                    }

                    
else
                    
{
                        
break;
                    }
                    
                }


                i 
= j -1;
            }

        }

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPFDataGrid 控件,要实现相同内容合并单元格,需要自定义一个合并单元格为,可以使用 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、付费专栏及课程。

余额充值