DatagridView如何使每列的行数不同? [英] DatagridView How to have a different row count per column?
问题描述
因此,我试图在 datagridview
中以特定格式显示数据。
所以我的格式是这样的:
ABC
1 1 1
2 2 x
3 xx
x表示没有单元格。
您可以看到每一列的行数都不同。我想在DatagridView或Dot Net Framework中的任何其他控件中实现相同的结果。
要在
So I was trying to display my data in specific format in a datagridview
.
So my format goes like this:
A B C
1 1 1
2 2 x
3 x x
the x means no cell.
As you can see each column has different row count. I want to achieve the same result in DatagridView or in any other control in the Dot Net Framework.
To expand on jdweng's answer, if for some reason you truly want:
[T]he x means no cell.
Then you can handle the DataGridView.CellPainting
event to effectively hide the empty cell. Mind you, it will start to look odd when null
cells are intermingled amidst valued cells - and not just at the row ends.
// ...
dt.Rows.Add(new object[] { 3, null, null });
this.dataGridView1.DataSource = dt;
this.dataGridView1.CellPainting += DataGridView1_CellPainting;
private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
DataGridViewCell cell = this.dataGridView1[e.ColumnIndex, e.RowIndex];
if (cell.Value == null || cell.Value is DBNull)
{
using (SolidBrush brush = new SolidBrush(this.dataGridView1.BackgroundColor))
{
e.Graphics.FillRectangle(brush, e.CellBounds);
}
e.Handled = true;
}
}
}
这篇关于DatagridView如何使每列的行数不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!