winform datagridview 使用 CellPainting 事件在滚动时未完全正常工作

winform datagridview using cellpainting event not fully working on scroll

提问人:sharon 提问时间:11/9/2023 更新时间:11/9/2023 访问量:17

问:

我有一个包含 50 多列和 100 多行的 datagridview,我在 DataGridView 中使用 CellPainting 事件向特定列和行添加边框,但在水平滚动时遇到更细的边框。 问题是:

  1. 向右滚动时,边框会变细。
  2. 滚动回起点时,边框将恢复到其原始宽度。
  3. 垂直滚动按预期工作。

在我使用单元格绘制事件后,某些功能变慢了。我尝试了薮猫,但仍然无法解决这个问题。请帮帮我。首先感谢大家。

我的代码:

public new void DoubleBuffered(DataGridView dgv, bool setting)
{
    Type dgvType = dgv.GetType();
    PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
    BindingFlags.Instance | BindingFlags.NonPublic);
    pi.SetValue(dgv, setting, null);
}

private void dgvSettingAll_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex < 0 || e.ColumnIndex < 0)
        return;

    if (e.RowIndex == -1 && e.ColumnIndex >= 0)
    {
        if (e.ColumnIndex < 13 || e.ColumnIndex == 20 || e.ColumnIndex == 23 || e.ColumnIndex == 31 || e.ColumnIndex == 35 || e.ColumnIndex == 38 || e.ColumnIndex == 47)
        {
            e.Paint(e.CellBounds, DataGridViewPaintParts.All);

            using (Pen pen = new Pen(Color.Black, 2))
            {
                Rectangle rect = e.CellBounds;
                rect.X = rect.Right - 1;
                e.Graphics.DrawLine(pen, rect.X, rect.Y, rect.X, rect.Bottom);
            }

            e.Handled = true;
        }
    }

    if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
    {
        e.Paint(e.CellBounds, DataGridViewPaintParts.Background);
    
        DataGridViewRow row = dgvSettingAll.Rows[e.RowIndex];
        DataGridViewCell cellMarketType = row.Cells["ColMarketType"];
        DataGridViewCell cellWType = row.Cells["ColWType"];

        if (e.ColumnIndex == 14 && cellMarketType.Value.ToString() != "S")
        {
            e.Paint(e.CellBounds, DataGridViewPaintParts.All);

            using (Pen pen = new Pen(Color.Black, 2))
            {
                Rectangle rect = e.CellBounds;

                e.Graphics.DrawLine(pen, rect.Left, rect.Top, rect.Left, rect.Bottom);

            }

            e.Handled = true;
        }

        if (e.ColumnIndex == 20 || e.ColumnIndex == 23 || e.ColumnIndex == 31 || e.ColumnIndex == 35 || e.ColumnIndex == 38 || e.ColumnIndex == 47)
        {
            e.Paint(e.CellBounds, DataGridViewPaintParts.All);

            using (Pen pen = new Pen(Color.Black, 2))
            {
                Rectangle rect = e.CellBounds;
                rect.X = rect.Right - 1;

                e.Graphics.DrawLine(pen, rect.X, rect.Top, rect.X, rect.Bottom);
            }

            e.Handled = true;
        }

        if (cellMarketType.Value != null && cellMarketType.Value.ToString() == "S" && e.ColumnIndex > 13)
        {
            e.Paint(e.CellBounds, DataGridViewPaintParts.All);

            using (Pen pen = new Pen(Color.Black, 3))
            {
                Rectangle rect = e.CellBounds;
                rect.Y = rect.Top;

                e.Graphics.DrawLine(pen, rect.Left, rect.Y, rect.Right, rect.Y);

                if (e.ColumnIndex == 20 || e.ColumnIndex == 23 || e.ColumnIndex == 31 || e.ColumnIndex == 35 || e.ColumnIndex == 38 || e.ColumnIndex == 47)
                {
                    rect.X = rect.Right - 1;
                    e.Graphics.DrawLine(pen, rect.X, rect.Top, rect.X, rect.Bottom);
                }
            }

            e.Handled = true;
        }

        if (e.RowIndex == 0 && e.ColumnIndex < 13)
        {
            e.Paint(e.CellBounds, DataGridViewPaintParts.All);

            using (Pen pen = new Pen(Color.Black, 2))
            {
                 Rectangle rect = e.CellBounds;
                 rect.X = rect.Right - 1;

                 e.Graphics.DrawLine(pen, rect.X, rect.Top, rect.X, rect.Bottom);
            }

            e.Handled = true;
        }

        if (e.RowIndex >= 0 && e.ColumnIndex > 13 && e.ColumnIndex < 19 && cellWType.Value.ToString() != "All")
        {

            e.Paint(e.CellBounds, DataGridViewPaintParts.All);

            using (Pen pen = new Pen(Color.Black, 1))
            {
                Rectangle rect = e.CellBounds;
                rect.X = rect.Right - 1;

                e.Graphics.DrawLine(pen, rect.X, rect.Top, rect.X, rect.Bottom);
            }

            using (Pen pen = new Pen(Color.Black, 3))
            {
                Rectangle rect = e.CellBounds;
                rect.Y = rect.Top;
                rect.Height = 2;

                e.Graphics.DrawLine(pen, rect.Left, rect.Top, rect.Right, rect.Top);
            }

            e.Handled = true;
        }

        if (e.RowIndex >= 0 && e.ColumnIndex == 19 && (cellWType.Value.ToString() == "All" || cellMarketType.Value.ToString() == "WI"))
        {
            e.Paint(e.CellBounds, DataGridViewPaintParts.All);

            using (Pen pen = new Pen(Color.Black, 2))
            {
                Rectangle rect = e.CellBounds;
                rect.X = rect.Right - 1;

                e.Graphics.DrawLine(pen, rect.X, rect.Top, rect.X, rect.Bottom);
            }

            using (Pen pen = new Pen(Color.Black, 3))
            {
                Rectangle rect = e.CellBounds;
                rect.Y = rect.Top;
                rect.Height = 2;

                e.Graphics.DrawLine(pen, rect.Left, rect.Top, rect.Right, rect.Top);
            }

            e.Handled = true;
        }
    }
}
C# WinForms DataGridView

评论


答: 暂无答案