如何:變更 Windows Form DataGridView 控制項中的框線和格線樣式
DataGridView透過 控制項,您可以自訂控制項框線和格線的外觀,以改善使用者體驗。 除了控制項內儲存格的框線樣式之外,您還可以修改格線色彩和控制項框線樣式。 您也可以為一般儲存格、資料列標題儲存格和資料行標題儲存格套用不同的儲存格框線樣式。
注意
格線色彩只會與 列舉的 DataGridViewCellBorderStyle 、 SingleHorizontal 和 SingleVertical 值以及 Single 列舉值 DataGridViewHeaderBorderStyle 搭配 Single 使用。 這些列舉的其他值會使用作業系統指定的色彩。 此外,當透過 方法在 Windows XP 和 Windows Server 2003 系列 Application.EnableVisualStyles 上啟用視覺化樣式時, GridColor 不會使用 屬性值。
以程式設計方式變更格線色彩
設定 GridColor 屬性。
this.dataGridView1.GridColor = Color.BlueViolet;
Me.dataGridView1.GridColor = Color.BlueViolet
以程式設計方式變更整個 DataGridView 控制項的框線樣式
將 BorderStyle 屬性設定為其中一個 BorderStyle 列舉值。
this.dataGridView1.BorderStyle = BorderStyle.Fixed3D;
Me.dataGridView1.BorderStyle = BorderStyle.Fixed3D
以程式設計方式變更 DataGridView 儲存格的框線樣式
設定 CellBorderStyle、 RowHeadersBorderStyle和 ColumnHeadersBorderStyle 屬性。
this.dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None; this.dataGridView1.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single; this.dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
With Me.dataGridView1 .CellBorderStyle = DataGridViewCellBorderStyle.None .RowHeadersBorderStyle = _ DataGridViewHeaderBorderStyle.Single .ColumnHeadersBorderStyle = _ DataGridViewHeaderBorderStyle.Single End With
範例
private void SetBorderAndGridlineStyles()
{
this.dataGridView1.GridColor = Color.BlueViolet;
this.dataGridView1.BorderStyle = BorderStyle.Fixed3D;
this.dataGridView1.CellBorderStyle =
DataGridViewCellBorderStyle.None;
this.dataGridView1.RowHeadersBorderStyle =
DataGridViewHeaderBorderStyle.Single;
this.dataGridView1.ColumnHeadersBorderStyle =
DataGridViewHeaderBorderStyle.Single;
}
Private Sub SetBorderAndGridlineStyles()
With Me.dataGridView1
.GridColor = Color.BlueViolet
.BorderStyle = BorderStyle.Fixed3D
.CellBorderStyle = DataGridViewCellBorderStyle.None
.RowHeadersBorderStyle = _
DataGridViewHeaderBorderStyle.Single
.ColumnHeadersBorderStyle = _
DataGridViewHeaderBorderStyle.Single
End With
End Sub
編譯程式碼
這個範例需要:
名為
dataGridView1
的 DataGridView 控制項。System、System.Windows.Forms 和 System.Drawing 組件的參考。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應