В форме 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];
}
}
Линии были нарисованы после запуска приложения.Однако линии не перерисовываются при прокрутке строк в сетках?Как заставить его перерисовывать линии при прокрутке?