2014-03-28 243 views

回答

1

沒有直接的方法來做到這一點。您已在CellPainting事件處理程序中繪製自己的邊框。

有一個類級別的變量來存儲點擊的列標題索引。

int myClickedColumnHeaderIndex = -1; 

訂閱以下活動。

dataGridView1.CellPainting += dataGridView1_CellPainting; 
dataGridView1.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_ColumnHeaderMouseClick); 

ColumnHeaderMouseClick處理程序使用類級別變量存儲列索引。

void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) 
{ 
    if (e.Button == MouseButtons.Left && e.Clicks == 1) 
    { 
     dataGridView1.InvalidateCell(myClickedColumnHeaderIndex, -1); // this to trigger paint of the old cell inorder to remove the border drawn earlier. 
     myClickedColumnHeaderIndex = e.ColumnIndex; 
    } 
} 

CellPainting事件處理程序,使用所需的顏色繪製邊框。

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
{ 
    if (e.RowIndex == -1 && e.ColumnIndex >= 0 && e.ColumnIndex == myClickedColumnHeaderIndex) 
    { 
     e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border); 
     using (Pen customPen = new Pen(Color.Blue, 2)) 
     { 
      Rectangle rect = e.CellBounds; 
      rect.Width -= 2; 
      rect.Height -= 2; 
      e.Graphics.DrawRectangle(customPen, rect); 
     } 
     e.Handled = true; 
    } 
} 
+0

感謝您對我有用的想法。可以設置頂部,左側,右側,底部邊界。 –

+0

@LhoreBansal - 是的,頂部,左側,右側,底部可以使用'e.Graphics.DrawLine'與'e.CellBounds'中的點分開繪製。如果它有效,你可以接受它作爲答案。 – Junaith

0

此代碼爲每個偶數列的標題單元和數據單元繪製垂直邊界。

private void DgvCalendar_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
     { 
      int columnIndex = 0; 

      if (e.RowIndex >= 0 && e.ColumnIndex >= columnIndex) 
      { 
       if (e.ColumnIndex % 2 == 0) 
       { 
        var brush = new SolidBrush(dgvCalendar.ColumnHeadersDefaultCellStyle.BackColor); 

        e.Graphics.FillRectangle(brush, e.CellBounds); 

        brush.Dispose(); 

        e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentBackground); 

        ControlPaint.DrawBorder(e.Graphics, e.CellBounds, 
         System.Drawing.Color.Transparent, 1, ButtonBorderStyle.Solid, 
         System.Drawing.Color.Transparent, 1, ButtonBorderStyle.Solid, 
         System.Drawing.Color.CornflowerBlue, 1, ButtonBorderStyle.Solid, 
         System.Drawing.Color.Transparent, 1, ButtonBorderStyle.Solid); 

        e.Handled = true; 
       } 
      } 

      if (e.RowIndex == -1 && e.ColumnIndex >= columnIndex) 
      { 
       if (e.ColumnIndex % 2 == 0) 
       { 
        e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border); 

        ControlPaint.DrawBorder(e.Graphics, e.CellBounds, 
          System.Drawing.Color.Transparent, 1, ButtonBorderStyle.Solid, 
          System.Drawing.Color.Transparent, 1, ButtonBorderStyle.Solid, 
          System.Drawing.Color.CornflowerBlue, 1, ButtonBorderStyle.Solid, 
          System.Drawing.Color.Transparent, 1, ButtonBorderStyle.Solid); 

        e.Handled = true; 

        e.Handled = true; 
       } 
      } 
     } 
相關問題