gpt4 book ai didi

c# - 如何在编辑 DataGridView 单元格周围绘制边框?

转载 作者:行者123 更新时间:2023-12-01 23:52:43 25 4
gpt4 key购买 nike

我想在 DataGridView 周围绘制红色边框正在编辑的单元格。

我已经成功地在选定的单元格周围绘制了红色边框,而未使用以下代码对其进行编辑:

private void Form1_Load(object sender, EventArgs e)
{
this.Width = 650;
this.Height = 250;
dataGridView1.Left = 5;
dataGridView1.Top = 5;
dataGridView1.Width = 600;
dataGridView1.Height = 175;

DataTable dt = new DataTable("Test Table");
dt.Columns.Add("Column 1");
dt.Columns.Add("Column 2");
dt.Columns.Add("Column 3");
dt.Columns.Add("Column 4");
dt.Columns.Add("Column 5");
dt.Rows.Add(dt.NewRow());
dt.Rows.Add(dt.NewRow());
dt.Rows.Add(dt.NewRow());
dt.Rows.Add(dt.NewRow());
dt.Rows.Add(dt.NewRow());
dataGridView1.DataSource = dt;

dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.MultiSelect = false;
dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White;
dataGridView1.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.dataGridView1_CellPainting);
dataGridView1.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.dataGridView1_EditingControlShowing);
}

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex != -1 && e.RowIndex != -1 && dataGridView1[e.ColumnIndex, e.RowIndex].Selected)
{
using (Brush borderBrush = new SolidBrush(Color.Red))
{
using (Pen borderPen = new Pen(borderBrush, 2))
{
Rectangle rectDimensions = e.CellBounds;
rectDimensions.Width -= 2;
rectDimensions.Height -= 2;
rectDimensions.X = rectDimensions.Left + 1;
rectDimensions.Y = rectDimensions.Top + 1;

e.Graphics.DrawRectangle(borderPen, rectDimensions);

e.Handled = true;
}
}
}
}

产生以下结果:

Image1

但是,当您编辑单元格时,会发生这种情况:

Image2

看来EditingControl正在将自己绘制在我的大部分红色边框的顶部。不幸的是,我找不到解决此问题的方法,因此我的红色边框将始终完全显示。

我该怎么做???

<小时/>


这是我迄今为止尝试过的:

1. 处理 EditingControlShowing()手动重绘事件 边框如下:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
Graphics gfx = e.Control.CreateGraphics();

using (Brush borderBrush = new SolidBrush(Color.Red))
{
using (Pen borderPen = new Pen(borderBrush, 2))
{
Rectangle rectDimensions = e.Control.ClientRectangle;
rectDimensions.Width -= 2;
rectDimensions.Height -= 2;
rectDimensions.X = rectDimensions.Left + 1;
rectDimensions.Y = rectDimensions.Top + 1;

gfx.DrawRectangle(borderPen, rectDimensions);
}
}
}

但这并没有画出任何东西。我尝试了一些变体,但它们仍然没有在这里画出任何东西。


2. 然后我尝试处理 Paint()事件EditingControl 像这样:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.Paint -= new PaintEventHandler(dataGridView1_EditingControl_Paint);
e.Control.Paint += new PaintEventHandler(dataGridView1_EditingControl_Paint);
}

void dataGridView1_EditingControl_Paint(object sender, PaintEventArgs e)
{
MessageBox.Show("Starting EditingControl Paint() Event...");
}

但是这个事件甚至没有触发。后来我在某处读到 EditingControl使用正常的 TextBox ,这不会触发 Paint()事件,因为它是由 Windows 处理的。


3. 最后,我决定不再尝试重新绘制另一个边框,而是 尝试通过调整 EditingControl 的大小来解决它成为 比我的边框小,希望边框能显示出来 它,像这样:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.Resize -= new EventHandler(dataGridView1_EditingControl_Resize);
e.Control.Resize += new EventHandler(dataGridView1_EditingControl_Resize);
}

void dataGridView1_EditingControl_Resize(object sender, EventArgs e)
{
dataGridView1.EditingControl.Left = 20;
}

但是,这给了我这个结果:

Image3

所以TextBox确实移到了左边,但似乎还有另一个 它下面的控件仍然挡住了我的红色边框。但是,无论如何我都找不到访问权限 到该控件来调整它的大小,所以这对我来说也不起作用。


4. 我还尝试使用上面 #1 中的代码重新绘制 Resize() 中的边框。事件,但仍然没有任何作用。虽然,使用 dataGridView1.EditingControl.BackColor = Color.Red;确实有效,所以我可以在这里格式化控件的某些部分,但似乎尝试绘制边框不是其中之一。

我想要做的就是在编辑单元格时保持单元格周围显示红色边框。你知道我该怎么做吗?

最佳答案

可以使用一些设置并绘制单元格的特定部分来完成。如此如此:

首先,设置CellBorderStyle在设计器中或仅在表单加载代码中设置为RaishedSunken:

this.dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Raised;

然后使用这些特定的排序规则绘制单元格:

  1. 仅绘制网格内容单元格,不绘制 ColumnHeader 单元格或 RowHeader
  2. 绘制选定单元格时,首先使用 e.Paint(...) 绘制除边框之外的所有部分;然后自己画边框
  3. 设置 e.Handled=true 以防止默认绘制
  4. 绘制非选定单元格时,首先使用 e.Paint(...) 绘制除边框之外的所有部分
  5. 使用网格背景颜色绘制第一行单元格的上边框和第一列单元格的左边框
  6. 使用网格线颜色绘制最后一行单元格的下边框和最后一列单元格的右边框
  7. 使用网格背景颜色绘制非最后一行单元格的下边框和非最后列单元格的右边框
  8. 使用网格线颜色绘制非第一行单元格的上边框和非最后列单元格的左边框9.设置e.Handled=true以防止默认绘制

这是选择后的结果截图

enter image description here

这是编辑单元格时的结果屏幕截图

enter image description here

这是单元格绘制事件的代码:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
//Draw only grid content cells not ColumnHeader cells nor RowHeader cells
if (e.ColumnIndex > -1 & e.RowIndex > -1)
{
//Pen for left and top borders
using (var backGroundPen = new Pen(e.CellStyle.BackColor, 1))
//Pen for bottom and right borders
using (var gridlinePen = new Pen(dataGridView1.GridColor, 1))
//Pen for selected cell borders
using (var selectedPen = new Pen(Color.Red, 1))
{
var topLeftPoint = new Point(e.CellBounds.Left, e.CellBounds.Top);
var topRightPoint = new Point(e.CellBounds.Right - 1, e.CellBounds.Top);
var bottomRightPoint = new Point(e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
var bottomleftPoint = new Point(e.CellBounds.Left, e.CellBounds.Bottom - 1);

//Draw selected cells here
if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Selected)
{
//Paint all parts except borders.
e.Paint(e.ClipBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border);

//Draw selected cells border here
e.Graphics.DrawRectangle(selectedPen, new Rectangle(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width - 1, e.CellBounds.Height - 1));

//Handled painting for this cell, Stop default rendering.
e.Handled = true;
}
//Draw non-selected cells here
else
{
//Paint all parts except borders.
e.Paint(e.ClipBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border);

//Top border of first row cells should be in background color
if (e.RowIndex == 0)
e.Graphics.DrawLine(backGroundPen, topLeftPoint, topRightPoint);

//Left border of first column cells should be in background color
if (e.ColumnIndex == 0)
e.Graphics.DrawLine(backGroundPen, topLeftPoint, bottomleftPoint);

//Bottom border of last row cells should be in gridLine color
if (e.RowIndex == dataGridView1.RowCount - 1)
e.Graphics.DrawLine(gridlinePen, bottomRightPoint, bottomleftPoint);
else //Bottom border of non-last row cells should be in background color
e.Graphics.DrawLine(backGroundPen, bottomRightPoint, bottomleftPoint);

//Right border of last column cells should be in gridLine color
if (e.ColumnIndex == dataGridView1.ColumnCount - 1)
e.Graphics.DrawLine(gridlinePen, bottomRightPoint, topRightPoint);
else //Right border of non-last column cells should be in background color
e.Graphics.DrawLine(backGroundPen, bottomRightPoint, topRightPoint);

//Top border of non-first row cells should be in gridLine color, and they should be drawn here after right border
if (e.RowIndex > 0)
e.Graphics.DrawLine(gridlinePen, topLeftPoint, topRightPoint);

//Left border of non-first column cells should be in gridLine color, and they should be drawn here after bottom border
if (e.ColumnIndex > 0)
e.Graphics.DrawLine(gridlinePen, topLeftPoint, bottomleftPoint);

//We handled painting for this cell, Stop default rendering.
e.Handled = true;
}
}
}
}

关于c# - 如何在编辑 DataGridView 单元格周围绘制边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32154847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com