自定义单元格合并
Custom cell merged
我需要在 gridview 中显示某些列的合并行的数据。
来自数据库的原始数据如:
请帮我显示网格视图:
- 由 tgl
合并的 transaksi 列
- 列 priority , price , crete by merged by 交易
如何使用 C# 为格式准备 gridview?请帮助我。
我刚刚选择了在 this topic 上发布的回复并添加了一行以实现您想要的。
bool IsTheSameCellValue(int column, int row)
{
// To compare only values on 1st and 2nd column (TGL, TRANSAKSI)
if (column > 1) return false;
DataGridViewCell cell1 = dataGridView[column, row];
DataGridViewCell cell2 = dataGridView[column, row - 1];
if (cell1.Value == null || cell2.Value == null)
{
return false;
}
return cell1.Value.ToString() == cell2.Value.ToString();
}
private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
if (e.RowIndex < 1 || e.ColumnIndex < 0)
return;
if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
{
e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
}
else
{
e.AdvancedBorderStyle.Top = dataGridView.AdvancedCellBorderStyle.Top;
}
}
private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex == 0)
return;
if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
{
e.Value = "";
e.FormattingApplied = true;
}
}
但是,使用此解决方案无法使合并单元格中的文本垂直居中,因为它只会擦除边框而不会完全重绘组件。
我建议您查看文档 - Tutorial: Cell Merging
To implement custom cell merge use the GridView.CellMerge event
handler. First, check if the correct column is being processed. Then,
obtain display texts for the two cells being compared. Finally,
indicate that cells are to be merged if their display texts match. Set
the CellMergeEventArgs.Handled parameter to true to override the
grid's default processing for this column.
示例:
using DevExpress.XtraGrid.Views.Grid;
// ...
private void gridView1_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e) {
GridView view = sender as GridView;
if(view == null) return;
if (e.Column == colCreatorID) {
string text1 = view.GetRowCellDisplayText(e.RowHandle1, colCreatorID);
string text2 = view.GetRowCellDisplayText(e.RowHandle2, colCreatorID);
e.Merge = (text1 == text2);
e.Handled = true;
}
}
在处理列时使用您自己的条件。例如取 transaksi 005
,在这种情况下,检查第 5 行和第 6 行的值,然后比较由创建的列是否相等取决于您的条件,将 e.Merge
设置为 true
。
我需要在 gridview 中显示某些列的合并行的数据。 来自数据库的原始数据如:
请帮我显示网格视图:
- 由 tgl 合并的 transaksi 列
- 列 priority , price , crete by merged by 交易
如何使用 C# 为格式准备 gridview?请帮助我。
我刚刚选择了在 this topic 上发布的回复并添加了一行以实现您想要的。
bool IsTheSameCellValue(int column, int row)
{
// To compare only values on 1st and 2nd column (TGL, TRANSAKSI)
if (column > 1) return false;
DataGridViewCell cell1 = dataGridView[column, row];
DataGridViewCell cell2 = dataGridView[column, row - 1];
if (cell1.Value == null || cell2.Value == null)
{
return false;
}
return cell1.Value.ToString() == cell2.Value.ToString();
}
private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
if (e.RowIndex < 1 || e.ColumnIndex < 0)
return;
if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
{
e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
}
else
{
e.AdvancedBorderStyle.Top = dataGridView.AdvancedCellBorderStyle.Top;
}
}
private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex == 0)
return;
if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
{
e.Value = "";
e.FormattingApplied = true;
}
}
但是,使用此解决方案无法使合并单元格中的文本垂直居中,因为它只会擦除边框而不会完全重绘组件。
我建议您查看文档 - Tutorial: Cell Merging
To implement custom cell merge use the GridView.CellMerge event handler. First, check if the correct column is being processed. Then, obtain display texts for the two cells being compared. Finally, indicate that cells are to be merged if their display texts match. Set the CellMergeEventArgs.Handled parameter to true to override the grid's default processing for this column.
示例:
using DevExpress.XtraGrid.Views.Grid;
// ...
private void gridView1_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e) {
GridView view = sender as GridView;
if(view == null) return;
if (e.Column == colCreatorID) {
string text1 = view.GetRowCellDisplayText(e.RowHandle1, colCreatorID);
string text2 = view.GetRowCellDisplayText(e.RowHandle2, colCreatorID);
e.Merge = (text1 == text2);
e.Handled = true;
}
}
在处理列时使用您自己的条件。例如取 transaksi 005
,在这种情况下,检查第 5 行和第 6 行的值,然后比较由创建的列是否相等取决于您的条件,将 e.Merge
设置为 true
。