在DataGridView中发生滚动时,单元格会重叠并绘制 [英] Cells gets overlapped and painted when Scroll happens in DataGridView
问题描述
最初,我在用户滚动时在单元格中显示数据,我需要在DataGridView中加载更多数据。
我正在使用DataGridView CellPainting绘制线条。
当我开始在datagridview中滚动时,单元格重叠并且完全改变了输出。
公共部分类Display:表格
{
public Display()
{
InitializeComponent();
LoadData();
}
//创建行和列并填充一些数据
private void LoadData()
{
int columnSize = 10;
DataGridViewColumn [] columnName = new DataGridViewColumn [columnSize];
for(int index = 0; index< columnSize; index ++)
{
columnName [index] = new DataGridViewTextBoxColumn();
if(index == 0)
{
columnName [index] .Name =名称;
columnName [index] .HeaderText =名称;
}
其他
{
columnName [index] .Name =(index).ToString();
columnName [index] .HeaderText =(index).ToString();
}
columnName [index] .FillWeight = 0.00001f;
columnName [index] .AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
dataGridView1.Columns.Add(columnName [index]);
}
for(int rowIndex = 0; rowIndex&column ;; rowIndex ++)
{
dataGridView1.Rows.Add((rowIndex + 1).ToString( ));
dataGridView1.Rows [rowIndex] .HeaderCell.Value =(rowIndex + 1).ToString();
}
}
私有无效dataGridView1_CellPainting(对象发送者,DataGridViewCellPaintingEventArgs e)
{
Rectangle rectPos1 = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex,false);
Pen graphPen =新的Pen(颜色。红色,1);
Graphics graphics = this.dataGridView1.CreateGraphics();
Point [] points =
{
新Point(rectPos1.Left,rectPos1.Bottom),
新Point(rectPos1.Right,rectPos1.Bottom),
new Point(rectPos1.Right,rectPos1.Top)
};
graphics.DrawLines(graphPen,points);
e.PaintContent(rectPos1);
e.Handled = true;
}
}
如何避免这种情况,请帮助我解决此问题。
几个问题。首先,几乎应该始终使用从PaintEventArgs获得的Graphics对象。 CreateGraphics是易于擦除的临时画布。您获得的参数之一是CellBounds矩形,因此您可以使用它。您的线实际上是在矩形的外部绘制的,并且您没有清除先前的内容,因此您的代码应如下所示:
矩形rectPos1 = e.CellBounds;
e.Graphics.FillRectangle(Brushes.White,rectPos1);
Graphics graphics = e.Graphics; // this.dataGridView1.CreateGraphics();
Point [] points =
{
新Point(rectPos1.Left,rectPos1.Bottom-1),
新Point(rectPos1.Right-1,rectPos1.Bottom-1 ),
新Point(rectPos1.Right-1,rectPos1.Top)
};
graphics.DrawLines(Pens.Red,points);
e.PaintContent(rectPos1);
e.Handled = true;
Initially I display data in cells as user scrolls I need to load more data in DataGridView.
I am using DataGridView CellPainting for drawing lines. When I start scrolling in datagridview the cells get overlapped and it completely changes the output.
public partial class Display : Form
{
public Display()
{
InitializeComponent();
LoadData();
}
// To create the rows and columns and fill some data
private void LoadData()
{
int columnSize = 10;
DataGridViewColumn[] columnName = new DataGridViewColumn[columnSize];
for (int index = 0; index < columnSize; index++)
{
columnName[index] = new DataGridViewTextBoxColumn();
if (index == 0)
{
columnName[index].Name = "Name";
columnName[index].HeaderText = "Name";
}
else
{
columnName[index].Name = (index).ToString();
columnName[index].HeaderText = (index).ToString();
}
columnName[index].FillWeight = 0.00001f;
columnName[index].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
dataGridView1.Columns.Add(columnName[index]);
}
for (int rowIndex = 0; rowIndex < columnSize; rowIndex++)
{
dataGridView1.Rows.Add((rowIndex + 1).ToString());
dataGridView1.Rows[rowIndex].HeaderCell.Value = (rowIndex + 1).ToString();
}
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
Rectangle rectPos1 = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
Pen graphPen = new Pen(Color.Red, 1);
Graphics graphics = this.dataGridView1.CreateGraphics();
Point[] points =
{
new Point(rectPos1.Left , rectPos1.Bottom),
new Point(rectPos1.Right, rectPos1.Bottom),
new Point(rectPos1.Right, rectPos1.Top)
};
graphics.DrawLines(graphPen, points);
e.PaintContent(rectPos1);
e.Handled = true;
}
}
Which I have shown in the below image
How can i avoid it please help me solve this issue.
A couple of issues. First and foremost, you should almost always use the provided Graphics object you get from the PaintEventArgs. CreateGraphics is a temporary canvas that gets easily erased. One of the parameters you get is the CellBounds rectangle, so you can use that. Your lines are actually drawing outside of the rectangle, and you aren't clearing the previous contents, so your code should look something like this:
Rectangle rectPos1 = e.CellBounds;
e.Graphics.FillRectangle(Brushes.White, rectPos1);
Graphics graphics = e.Graphics; // this.dataGridView1.CreateGraphics();
Point[] points =
{
new Point(rectPos1.Left , rectPos1.Bottom - 1),
new Point(rectPos1.Right - 1, rectPos1.Bottom - 1),
new Point(rectPos1.Right - 1, rectPos1.Top)
};
graphics.DrawLines(Pens.Red, points);
e.PaintContent(rectPos1);
e.Handled = true;
这篇关于在DataGridView中发生滚动时,单元格会重叠并绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!