GridControl.TableView单元格合并
xaml 文件中先添加一个GridControl ,数据源再.cs文件中设定
<Grid>
<UniformGrid>
<dxg:GridControl Name="grid">
<dxg:GridControl.View>
<dxg:TableView
Name="tableView"
AllowEditing="False"
AutoWidth="True" />
</dxg:GridControl.View>
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="A" />
<dxg:GridColumn FieldName="B" />
<dxg:GridColumn FieldName="C" />
</dxg:GridControl.Columns>
</dxg:GridControl>
</UniformGrid>
</Grid>
.cs代码
```csharp
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("A");
dt.Columns.Add("B");
dt.Columns.Add("C");
for (int m = 0; m < 15; m++)
{
if (m < 3)
{
dt.Rows.Add("A0", $"B0",$"C{m}");
}
else if (m < 6) {
dt.Rows.Add("A0", $"B1", $"C{m}");
}
else if (m < 10)
{
dt.Rows.Add("A1", $"B2", $"C{m}");
}
else
{
dt.Rows.Add("A2", $"B3", $"C{m}");
}
}
grid.ItemsSource = dt;
}
在列数据AllowCellMerge = true
<Grid>
<UniformGrid>
<dxg:GridControl Name="grid">
<dxg:GridControl.View>
<dxg:TableView
Name="tableView"
AllowEditing="False"
AutoWidth="True" />
</dxg:GridControl.View>
<dxg:GridControl.Columns>
<dxg:GridColumn AllowCellMerge="True" FieldName="A" />
<dxg:GridColumn AllowCellMerge="True" FieldName="B" />
<dxg:GridColumn AllowCellMerge="True" FieldName="C" />
</dxg:GridControl.Columns>
</dxg:GridControl>
</UniformGrid>
</Grid>
A列依据B列再进行一次分组
B列再进行一次分组
<Grid>
<UniformGrid>
<dxg:GridControl Name="grid">
<dxg:GridControl.View>
<dxg:TableView
Name="tableView"
AllowEditing="False"
AutoWidth="True"
CellMerge="TableView_CellMerge" />
</dxg:GridControl.View>
<dxg:GridControl.Columns>
<dxg:GridColumn AllowCellMerge="True" FieldName="A" />
<dxg:GridColumn AllowCellMerge="True" FieldName="B" />
<dxg:GridColumn AllowCellMerge="True" FieldName="C" />
</dxg:GridControl.Columns>
</dxg:GridControl>
</UniformGrid>
</Grid>
private void TableView_CellMerge(object sender, DevExpress.Xpf.Grid.CellMergeEventArgs e)
{
var rowHandle1 = e.RowHandle1;
var rowHandle2 = e.RowHandle2;
string strA1 = (grid.GetRow(rowHandle1) as DataRowView).Row["A"].ToString();
string strA2 = (grid.GetRow(rowHandle2) as DataRowView).Row["A"].ToString();
string strB1 = (grid.GetRow(rowHandle1) as DataRowView).Row["B"].ToString();
string strB2 = (grid.GetRow(rowHandle2) as DataRowView).Row["B"].ToString();
if (e.Column.FieldName == "A")
{
var tv = sender as TableView;
var gc = tv.DataControl;
var r1 = e.CellValue1.ToString();
var r2 = e.CellValue2.ToString();
e.Merge = (strA1 == strA2) && (strB1 == strB2);
e.Handled = true;
}
}