刷新DataGridView DataSource后,Winforms图形消失

时间:2017-07-11 17:06:50

标签: c# winforms datagridview graphics

所以我继承了这个WinForms应用程序,它做了很多图形绘制。通过反复调用以下方法在表单上绘制一个排序网格:

    private void DrawCell(int startx, int starty, int width, int height, System.Drawing.Color fill, string text = "")
    {
        using (System.Drawing.Graphics formGraphics = this.CreateGraphics())
        {
            using (System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(fill))
            {
                Rectangle temp = new Rectangle(startx, starty, width, height);
                formGraphics.FillRectangle(myBrush, temp);
                ControlPaint.DrawBorder(formGraphics, temp, System.Drawing.Color.Black, ButtonBorderStyle.Solid);

                if (text != "")
                {
                    Font font2 = new Font("Arial", 8, FontStyle.Bold, GraphicsUnit.Point);
                    TextRenderer.DrawText(formGraphics, text, font2, temp, Color.Blue);
                }
            }
        }
    }

绘制此网格的过程"通过选择ComboBox中的项目来启动,该项目调用" DrawGrid"循环遍历已知数量的行和列的方法,并在循环中调用上述方法。

我在表单中添加了一个DataGridView控件。除了更新上面的" grid"之外,我现在想要更新DataGridView。所以在ComboBox的SelectedValueChanged事件中,我还调用一个方法来获取数据值(简单的一维数字数组)并用数组更新DGV的数据源。

所以这就是问题所在:当我在ComboBox中选择一个项目时,"假的"网格绘制并立即消失第一次,但此后一切正常。 "假"然后网格正确绘制并且不会消失。

无论我在哪个顺序画出"假的"网格或更新DataGridView。如果我注释掉设置DGV的数据源的行,那么"假的"网格工作正常,甚至是第一次。

为什么会发生这种情况的任何想法?我根本没有使用表单的OnPaint事件。

...谢谢

0 个答案:

没有答案