最初我以用戶滾動的方式在單元格中顯示數據我需要在DataGridView中加載更多數據。當DataGridView發生滾動時,單元格被重疊並着色
我正在使用DataGridView CellPainting繪製線條。 當我開始在datagridview中滾動時,單元格重疊並且它完全改變了輸出。
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;
}
}
其中我下面的圖片中顯示
我怎樣才能避免它,請幫我解決這個問題。
請問您可以發佈實際代碼而不是鏈接?你是否也嘗試使用調試程序逐步完成代碼..?也許你正在使用不正確的EventHandler來顯示 – MethodMan
對不起MethodMan,忘了發佈代碼。我已更新原始帖子 – Sharath
我試過調試,不知道爲什麼它在滾動期間再次重新繪製 – Sharath