-1

ここに画像の説明を入力

datagridview の列ヘッダーを描画し、スクロール イベントで再描画イベントを呼び出しましたが、適切に再描画されていないようです。ペイントされた長方形のテキストがばらばらになります (2 番目の画像を参照)。これが私のコードです。

 void dataGridView1_Scroll(object sender, ScrollEventArgs e)
 {
     Rectangle rtHeader = this.dataGridView1.DisplayRectangle;
     rtHeader.Y += 0;
     rtHeader.Height = this.dataGridView1.ColumnHeadersHeight;
 }
 Rectangle r1;
 void dataGridView1_Paint(object sender, PaintEventArgs e)
 {
     string[] monthes = { "APPLE", "MANGO", "CHERRY", "GRAPES", "PINEAPPLE" };
     for (int j = 0; j < this.dataGridView1.ColumnCount; )
     {
         r1 = this.dataGridView1.GetCellDisplayRectangle(j, -1, true);
         int w2 = this.dataGridView1.GetCellDisplayRectangle(j + 1, -1, true).Width;
         r1.X += -2;
         r1.Y += 30;
         r1.Width = r1.Width + w2 - 1;
         r1.Height = r1.Height / 3 - 2;
         e.Graphics.FillRectangle(new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1);
         StringFormat format = new StringFormat();
         format.Alignment = StringAlignment.Center;
         format.LineAlignment = StringAlignment.Center;
         e.Graphics.DrawRectangle(new Pen(Color.Black), r1);
         e.Graphics.DrawString(monthes[j / 2], this.dataGridView1.ColumnHeadersDefaultCellStyle.Font, new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor), r1, format);
         j += 2;
     }
     string[] year = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY" };
     //for (int i = 0; i < this.dataGridView1.ColumnCount; )
     //{
           Rectangle rec = this.dataGridView1.GetCellDisplayRectangle(0, -1, true);
           int wid = this.dataGridView1.GetCellDisplayRectangle(1, -1, true).Width;
           rec.X += -2;
           rec.Y += 1;
           rec.Width = this.dataGridView1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible);
           rec.Height = rec.Height / 3 - 2;
           e.Graphics.FillRectangle(new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), rec);
           StringFormat frm = new StringFormat();
           frm.Alignment = StringAlignment.Center;
           frm.LineAlignment = StringAlignment.Center;
           e.Graphics.DrawRectangle(new Pen(Color.Black), rec);
           e.Graphics.DrawString("Favourite fruits", new Font("Times new roman", 16, FontStyle.Regular), new SolidBrush(Color.CornflowerBlue), rec, frm);
 }

ここに画像の説明を入力

4

2 に答える 2

0
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
    {
        /*
        Rectangle rtHeader = this.dataGridView1.DisplayRectangle;
        rtHeader.Y += 0;
        rtHeader.Height = this.dataGridView1.ColumnHeadersHeight;
        this.dataGridView1.Invalidate(rtHeader);

        */
        this.dataGridView1.Invalidate();
    }
于 2013-07-18T00:42:06.437 に答える