在WinForms表单中,我有一个表布局面板和两个DataGridViews(DGV1
和DGV2
)。在两个数据网格视图之间绘制线。
private void TableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
DrawLines(e.Graphics);
}
private void DrawLines(Graphics g)
{
var DGV1Rows = GetVisibleDataGridViewRows(DGV1);
var DGV2Rows = GetVisibleDataGridViewRows(DGV2);
var lines = GetLines(DGV1Rows, DGV2Rows); // Find all relations between two grid views
foreach (var line in lines)
{
g.DrawLine(line.Pen, line.R1, line.C1, line.R2, line.C2);
}
}
private IEnumerable<(int R1, int C1, int R2, int C2, Pen Pen)> GetLines(
IEnumerable<DataGridViewRow> Rows1,
IEnumerable<DataGridViewRow> Rows2)
{ .... // return the lines between the rows of the two grids.
// the line will point to the bottom of the grids if the rows are not visible in another side
}
IEnumerable<DataGridViewRow> GetVisibleRows(DataGridView dgv)
{
var visibleRowsCount = dgv.DisplayedRowCount(true);
var firstVisibleRowIndex = dgv.FirstDisplayedCell.RowIndex;
for (int i = firstVisibleRowIndex; i < firstVisibleRowIndex + visibleRowsCount; i++)
{
yield return dgv.Rows[i];
}
}
在应用程序运行之后绘制了线条。但是,如果滚动网格中的行,这些行不会重绘吗?滚动时如何强制其重画线条?