C# 自定义DataGridViewRow问题
我正在尝试创建一个自定义DataGridViewRow,它将充当分隔符,将数据划分为多个组。我通过如下方式重写DataGridViewRow类中的PainCells方法实现了这一点:C# 自定义DataGridViewRow问题,c#,.net,winforms,datagridview,C#,.net,Winforms,Datagridview,我正在尝试创建一个自定义DataGridViewRow,它将充当分隔符,将数据划分为多个组。我通过如下方式重写DataGridViewRow类中的PainCells方法实现了这一点: protected override void PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, int rowIndex, DataGridViewElementStates rowState, bool isFirs
protected override void PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, int rowIndex, DataGridViewElementStates rowState, bool isFirstDisplayedRow, bool isLastVisibleRow, DataGridViewPaintParts paintParts)
{
graphics.FillRectangle(new SolidBrush(BackgroundColor),rowBounds);
StringFormat format = new StringFormat {Alignment = StringAlignment.Center,LineAlignment = StringAlignment.Center};
graphics.DrawString(Text, this.DataGridView.Font, new SolidBrush(ForeColor), rowBounds,format);
}
其中,Text、BackgroundColor和ForeColor是我的SeparatorDataGridViewRow类的属性。这些排看起来和我想要的一模一样。但这只是封面,因为在绘制的矩形下方仍然有原始的datagrid单元格。特别是我添加的带有delete按钮表单DataGridViewButtonColumn的单元格。因此,如果我点击按钮所在的单元格,我的分隔行就会被删除
我不认为这是实现我想要实现的目标的正确方法,所以我很高兴你们能给我指明正确的方向,或者告诉我这个解决方案可以做什么,以阻止用户对这一行做任何事情的可能性,无论在绘制的矩形下方是什么
我在创建自定义控件方面没有什么经验,所以我不知道要重写什么方法,或者添加什么来实现某些目标。
感谢所有帮助:好的,我部分解决了我的问题。我不知道这个解决方案是否好,它给我带来了另一个神秘的问题 在我的分隔符DataGridViewRow上,我将标记设置为分隔符。然后,我创建了自己的DataGridView控件,在该控件中重写OnUserDeletingRow方法:
protected override void OnUserDeletingRow(DataGridViewRowCancelEventArgs e)
{
if (e.Row.Tag != null && e.Row.Tag.ToString().Equals("Separator"))
{
e.Cancel = true;
return;
}
base.OnUserDeletingRow(e);
}
这样,用户就不能删除我的分隔行。但现在奇怪的事情发生了。我有一个DataGridView,如下所示:
protected override void PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, int rowIndex, DataGridViewElementStates rowState, bool isFirstDisplayedRow, bool isLastVisibleRow, DataGridViewPaintParts paintParts)
{
graphics.FillRectangle(new SolidBrush(BackgroundColor),rowBounds);
StringFormat format = new StringFormat {Alignment = StringAlignment.Center,LineAlignment = StringAlignment.Center};
graphics.DrawString(Text, this.DataGridView.Font, new SolidBrush(ForeColor), rowBounds,format);
}
当我选择名为2的行并将其删除时,一切正常:
它适用于所有行。但是,如果我选择任何分隔行并按Delete键,它将不会被删除,但如果我删除它上面的任何行,则会发生以下情况:
在本例中,我选择了分隔符2,然后删除了名为2的行。这很奇怪,因为实际行正在被删除,而所选行实际上是分隔符2行。这是某种油漆问题。但我不知道为什么只有在我之前尝试删除分隔行时才会发生这种情况。在删除上述任何一项之前尝试删除分隔行时,绘制过程中会发生什么变化?好的,我部分解决了问题。我不知道这个解决方案是否好,它给我带来了另一个神秘的问题 在我的分隔符DataGridViewRow上,我将标记设置为分隔符。然后,我创建了自己的DataGridView控件,在该控件中重写OnUserDeletingRow方法:
protected override void OnUserDeletingRow(DataGridViewRowCancelEventArgs e)
{
if (e.Row.Tag != null && e.Row.Tag.ToString().Equals("Separator"))
{
e.Cancel = true;
return;
}
base.OnUserDeletingRow(e);
}
这样,用户就不能删除我的分隔行。但现在奇怪的事情发生了。我有一个DataGridView,如下所示:
protected override void PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, int rowIndex, DataGridViewElementStates rowState, bool isFirstDisplayedRow, bool isLastVisibleRow, DataGridViewPaintParts paintParts)
{
graphics.FillRectangle(new SolidBrush(BackgroundColor),rowBounds);
StringFormat format = new StringFormat {Alignment = StringAlignment.Center,LineAlignment = StringAlignment.Center};
graphics.DrawString(Text, this.DataGridView.Font, new SolidBrush(ForeColor), rowBounds,format);
}
当我选择名为2的行并将其删除时,一切正常:
它适用于所有行。但是,如果我选择任何分隔行并按Delete键,它将不会被删除,但如果我删除它上面的任何行,则会发生以下情况:
在本例中,我选择了分隔符2,然后删除了名为2的行。这很奇怪,因为实际行正在被删除,而所选行实际上是分隔符2行。这是某种油漆问题。但我不知道为什么只有在我之前尝试删除分隔行时才会发生这种情况。在删除上述任何一行之前,当我尝试删除分隔行时,绘制过程中会发生什么变化?是否有任何方法告诉DataGridView不要在给定行中绘制单元格。还是在给定类型的行中?我找不到任何方法对此负责。DataGridView onPaint方法只提供ClipRectangle和Graphics作为参数,因此我无法确定他正在绘制哪一行。是否有任何方法告诉DataGridView不要在给定行中绘制单元格。还是在给定类型的行中?我找不到任何方法对此负责。DataGridView onPaint方法只提供ClipRectangle和Graphics作为参数,因此我无法确定绘制的是哪一行。