定義兩個樣式對象:
//定義兩種行樣式 private DataGridViewCellStyle m_RowStyleNormal; private DataGridViewCellStyle m_RowStyleAlternate;
在窗體加載的時候對樣式進行設置:
/// <summary> /// 設置行樣式 /// </summary> private void SetRowStyle() { //可根據需要設置更多樣式屬性,如字體、對齊、前景色、背景色等 this.m_RowStyleNormal = new DataGridViewCellStyle(); this.m_RowStyleNormal.BackColor = Color.LightBlue; this.m_RowStyleNormal.SelectionBackColor = Color.LightSteelBlue; this.m_RowStyleAlternate = new DataGridViewCellStyle(); this.m_RowStyleAlternate.BackColor = Color.LightGray; this.m_RowStyleAlternate.SelectionBackColor = Color.LightSlateGray; }
定義演示數據:
/// <summary> /// 綁定數據 /// </summary> private void BindData() { //建立一個DataTable並填充數據,然后綁定到DataGridView控件上 m_GradeTable = new DataTable(); m_GradeTable.Columns.Add("Class", typeof(string)); m_GradeTable.Columns.Add("Name", typeof(string)); m_GradeTable.Columns.Add("Grade", typeof(int)); m_GradeTable.Rows.Add(new string[] { "Class1", "Jim", "89" }); m_GradeTable.Rows.Add(new string[] { "Class1", "Jack", "77" }); m_GradeTable.Rows.Add(new string[] { "Class1", "Bill", "91" }); m_GradeTable.Rows.Add(new string[] { "Class2", "Tom", "58" }); m_GradeTable.Rows.Add(new string[] { "Class2", "Rose", "95" }); m_GradeTable.Rows.Add(new string[] { "Class3", "Peter", "64" }); m_GradeTable.Rows.Add(new string[] { "Class3", "David", "82" }); m_GradeTable.Rows.Add(new string[] { "Class3", "Eric", "68" }); m_GradeTable.Rows.Add(new string[] { "Class3", "Lily", "79" }); this.bdsGrade.DataSource = m_GradeTable; }
在DataGridView控件的CellFormatting事件中實現設置行樣式、單元格樣式和行號:
private void dgvDataTable_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { //在此對行樣式進行設置 if (e.ColumnIndex == this.dgvGrade.Columns["ColumnClass"].Index)//根據班級設置行樣式 { DataGridViewRow CurrentRow = this.dgvGrade.Rows[e.RowIndex]; CurrentRow.HeaderCell.Value = Convert.ToString(e.RowIndex + 1);//顯示行號,也可以設置成顯示其他信息 //CurrentRow.HeaderCell.ToolTipText = "當前第" + Convert.ToString(e.RowIndex + 1) + "行";//設置ToolTip信息 //以下為根據上一行內容判斷所屬組的效果 if (e.RowIndex == 0)//首行必須特殊處理,將其設置為常規樣式 { CurrentRow.DefaultCellStyle = this.m_RowStyleNormal; } else { //判斷和上一行是否屬於同一個班級,如果是則設置相同樣式,否則設置另一種樣式 //需要定義兩個DataGridViewCellStyle,用於交替顯示,也可以根據需要隱藏一些和上一行重復的信息 //這里當兩行是同一個班級時,將下一行的班級信息隱藏掉,選中時則顯示班級信息 if (CurrentRow.Cells[e.ColumnIndex].Value != DBNull.Value && CurrentRow.Cells[e.ColumnIndex].Value != null && CurrentRow.Cells[e.ColumnIndex].Value.ToString() == this.dgvGrade.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString()) { CurrentRow.DefaultCellStyle = this.dgvGrade.Rows[e.RowIndex - 1].DefaultCellStyle;//設置和上一行的樣式相同 CurrentRow.Cells[e.ColumnIndex].Style.ForeColor = CurrentRow.DefaultCellStyle.BackColor;//用前景色隱藏信息 //如果需要選中時顯示完整信息則注釋該下面一行 //CurrentRow.Cells[e.ColumnIndex].Style.SelectionForeColor = CurrentRow.DefaultCellStyle.SelectionBackColor;//選中時也使前景色等於背景色,將文字隱藏掉 } else//當前行和上一行不屬於同一個班級時 { if (this.dgvGrade.Rows[e.RowIndex - 1].DefaultCellStyle == this.m_RowStyleNormal)//根據上一行的樣式設置當前行的樣式 CurrentRow.DefaultCellStyle = this.m_RowStyleAlternate; else CurrentRow.DefaultCellStyle = this.m_RowStyleNormal; } }//if(e.RowIndex == 0) } else if (e.ColumnIndex == this.dgvGrade.Columns["ColumnGrade"].Index)//根據成績設置單元格樣式 { if (this.dgvGrade.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != DBNull.Value && Convert.ToInt32(this.dgvGrade.Rows[e.RowIndex].Cells[e.ColumnIndex].Value) < 60)//對不及格的成績設置特殊樣式 { this.dgvGrade.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;//設置小於60的數字顯示為紅色 this.dgvGrade.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.SelectionForeColor = Color.Red; this.dgvGrade.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.Alignment = DataGridViewContentAlignment.MiddleRight; } } }
在DataGridView控件的RowPostPaint事件中實現行標題圖標繪制和提示信息設置:
//根據內容設置行標頭 private void dgvDataTable_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { if (this.dgvGrade.Rows[e.RowIndex].Cells["ColumnGrade"].Value == DBNull.Value) return; int intGrade = Convert.ToInt32(this.dgvGrade.Rows[e.RowIndex].Cells["ColumnGrade"].Value);//獲取成績 Image RowIcon;//標頭圖標 string strToolTip;//提示信息 if (intGrade >= 90) { RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeA;//從資源文件中獲取圖片 strToolTip = "Grade A"; } else if (intGrade >= 80) { RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeB; strToolTip = "Grade B"; } else if (intGrade >= 70) { RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeC; strToolTip = "Grade C"; } else if (intGrade >= 60) { RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeD; strToolTip = "Grade D"; } else { RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeF; strToolTip = "Grade F"; } e.Graphics.DrawImage(RowIcon, e.RowBounds.Left + this.dgvGrade.RowHeadersWidth - 20, e.RowBounds.Top + 4, 16, 16);//繪制圖標 this.dgvGrade.Rows[e.RowIndex].HeaderCell.ToolTipText = strToolTip;//設置提示信息 }