DataGridView - TopLeftHeaderCell datagridview上的Clear Paint

时间:2014-02-03 04:18:32

标签: c# datagridview paint

我正在实现事件绘制以在datagridview上的所有单元格中绘制字符串并填充矩形。 但它也在topleftheadercell上画。我如何摆脱图像??

这是我的活动画画

private void dataGridView1_Paint(object sender, PaintEventArgs e)
{
    ///
    /// Menggambar Header Column menjadi terlihat seperti di merge cells
    ///

    string[] monthes = (string[])myKaryawan.getKaryawan().ToArray(typeof(string));
    for (int j = 0; j < myKaryawan.CountKaryawan()* 2; )
    {

        Image newImage = Image.FromFile(Application.StartupPath+"\\SampImag.jpg",true);

        Rectangle recBackground = this.dataGridView1.GetCellDisplayRectangle(j, -1, true);
        Rectangle recImage = this.dataGridView1.GetCellDisplayRectangle(j, -1, true);
        Rectangle recString = this.dataGridView1.GetCellDisplayRectangle(j, -1, true);
        int w2 = this.dataGridView1.GetCellDisplayRectangle(j + 1, -1, true).Width;
        recBackground.X += 1;
        recBackground.Y += 1;
        recImage.X += 1;
        recImage.Y += 1;
        recString.X += (recString.Width + w2) / 2;
        recString.Y += 1;

        recBackground.Width = recBackground.Width + w2 - 2;
        recBackground.Height = recBackground.Height / 2 - 2;
        recImage.Width = (recImage.Width + w2) / 2 - 2;
        recImage.Height = recImage.Height / 2 - 2;
        recString.Width = (recString.Width + w2) / 2 - 2;
        recString.Height = recString.Height / 2 - 2;

        e.Graphics.FillRectangle(new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), recBackground);
        StringFormat format = new StringFormat();
        format.Alignment = StringAlignment.Center;
        format.LineAlignment = StringAlignment.Center;
        e.Graphics.DrawString(monthes[j / 2],
            this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,
            new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
            recString,
            format);
        e.Graphics.DrawImage(newImage, recImage);

        j += 2;
    }
    ///
    /// Initializing variable yang akan digunakan untuk menggambar
    /// Flag untuk menandakan posisi koordinat untuk looping gambar
    ///

    string awal = dataGridView1.Rows[0].HeaderCell.Value.ToString().Substring(6).ToString();
    string akhir = dataGridView1.Rows[6].HeaderCell.Value.ToString().Substring(6).ToString();
    tglAwal = Convert.ToDateTime(awal);
    tglAkhir = Convert.ToDateTime(akhir);
    var split = new String[25];
    int countRows = myActivity.getActivityPagi(tglAwal, tglAkhir).Tables[0].Rows.Count;
    int countColumns = myActivity.getActivityPagi(tglAwal, tglAkhir).Tables[0].Columns.Count;
    Font font1 = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point);
    Font font2 = new Font("Arial", 8, FontStyle.Regular, GraphicsUnit.Point);
    int[,] flag = new int[dataGridView1.Rows.Count, dataGridView1.Columns.Count];
    int[,] flag2 = new int[dataGridView1.Rows.Count, dataGridView1.Columns.Count];
    int[,] flag3 = new int[dataGridView1.Rows.Count, dataGridView1.Columns.Count];

    ///
    /// Draw Background Colour dan Draw String
    /// Pertama Looping semua isi data set kedalam string array
    /// Kemudian Looping per Cell untuk menggambar Stringnya dan Fill Rectangle
    /// 

    string[,] activity = new string[myActivity.getActivityPagi(tglAwal, tglAkhir).Tables[0].Rows.Count, 6];
    for (int i = 0; i < countRows; i++)
    {
        for (int j = 0; j < countColumns; j++)
        {
            activity[i, j] = myActivity.getActivityPagi(tglAwal, tglAkhir).Tables[0].Rows[i][j].ToString();
        }
    }
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
        for (int j = 0; j < dataGridView1.Columns.Count; j++)
        {
            for (int k = 0; k < countRows; k++)
            {
                headerValue = dataGridView1.Rows[i].HeaderCell.Value.ToString();
                split = headerValue.Split(',');
                time = DateTime.Parse(split[1]);
                if (time == Convert.ToDateTime(activity[k, 5]) && dataGridView1.Columns[j].Name == activity[k, 3] + activity[k, 4])
                {
                    Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(j, i, true);
                    Rectangle r2 = this.dataGridView1.GetCellDisplayRectangle(j, i, true);
                    Rectangle r3 = this.dataGridView1.GetCellDisplayRectangle(j, i, true);
                    int w2 = this.dataGridView1.GetCellDisplayRectangle(j, i, true).Width;
                    r1.X += 1;
                    r1.Y += flag[i, j]+1;
                    r1.Width = 65;
                    r1.Height = 15;
                    r2.X += 1;
                    r2.Y += flag2[i, j]+15;
                    r2.Width = 65;
                    r2.Height = 15;
                    r3.X += 1;
                    r3.Y += flag3[i, j] + 30;
                    r3.Width = 70;
                    r3.Height = 45;

                    flag[i, j] = flag[i, j]+75;
                    flag2[i, j] = flag2[i, j] + 75;
                    flag3[i, j] = flag3[i, j] + 75;

                        if (flag3[i, j] == dataGridView1.Rows[i].MinimumHeight -2)
                    {
                        dataGridView1.Rows[i].MinimumHeight += 75;
                    }
                    StringFormat format = new StringFormat();
                    StringFormat format2 = new StringFormat();
                    format2.Alignment = StringAlignment.Near;
                    format2.LineAlignment = StringAlignment.Near;
                    format.Alignment = StringAlignment.Center;
                    format.LineAlignment = StringAlignment.Center;
                    if (coloring.getColor(activity[k, 1]) == Color.Black)
                    {
                        e.Graphics.FillRectangle(new SolidBrush(Color.White), r1);
                        e.Graphics.DrawString(activity[k, 0],
                            font1,
                            new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
                            r1,
                            format2);
                        e.Graphics.FillRectangle(new SolidBrush(coloring.getColor(activity[k, 1])), r2);
                        e.Graphics.DrawString(activity[k, 1],
                            font1,
                            new SolidBrush(Color.White),
                            r2,
                            format);
                        e.Graphics.DrawString(activity[k, 2],
                            font2,
                            new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
                            r3,
                            format2);
                    }
                    else
                    {
                        e.Graphics.FillRectangle(new SolidBrush(Color.White), r1);
                        e.Graphics.DrawString(activity[k, 0],
                            font1,
                            new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
                            r1,
                            format2);
                        e.Graphics.FillRectangle(new SolidBrush(coloring.getColor(activity[k, 1])), r2);
                        e.Graphics.DrawString(activity[k, 1],
                            font1,
                            new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
                            r2,
                            format);
                        e.Graphics.DrawString(activity[k, 2],
                            font2,
                            new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
                            r3,
                            format2);
                    }

                }

            }

        }

    }

}

1 个答案:

答案 0 :(得分:1)

问题解决了,我只是在事件滚动中将datagridview.Invalidate();更改为

for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
    this.dataGridView1.InvalidateColumn(i);
}