-
大小: 0.03M文件類型: .rar金幣: 1下載: 0 次發布日期: 2020-12-26
- 語言: C#
- 標簽: GridView??DataGridView??
資源簡介
DataGridViewRowStyle 核心用法
截圖:
核心代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace TestDataGridViewRowStyle { public partial class Form1 : Form { //定義兩種行樣式 private DataGridViewCellStyle m_RowStyleNormal; private DataGridViewCellStyle m_RowStyleAlternate; //成績單DataTable private DataTable m_GradeTable; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.dgvGrade.AutoGenerateColumns = false; this.SetRowStyle(); this.BindData(); } /// <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; } 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; } } } //根據內容設置行標頭 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;//設置提示信息 } private void dgvGrade_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex >=0 && e.ColumnIndex == 2) { 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 img; if (intGrade >= 90) { img = TestDataGridViewRowStyle.Properties.Resources.high; } else if (intGrade >= 80) { img = TestDataGridViewRowStyle.Properties.Resources.arrow; } else if (intGrade >= 70) { img = TestDataGridViewRowStyle.Properties.Resources.up; } else if (intGrade >= 60) { img = TestDataGridViewRowStyle.Properties.Resources.down; } else { img = TestDataGridViewRowStyle.Properties.Resources.low; } Rectangle newRect = new Rectangle(e.CellBounds.X 3, e.CellBounds.Y 5, e.CellBounds.Height - 15, e.CellBounds.Height - 12); using (Brush gridBrush = new SolidBrush(this.dgvGrade.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor)) { using (Pen gridLinePen = new Pen(gridBrush, 2)) { // Erase the cell. e.Graphics.FillRectangle(backColorBrush, e.CellBounds); //劃線 Point p1 = new Point(e.CellBounds.Left e.CellBounds.Width, e.CellBounds.Top); Point p2 = new Point(e.CellBounds.Left e.CellBounds.Width, e.CellBounds.Top e.CellBounds.Height); Point p3 = new Point(e.CellBounds.Left, e.CellBounds.Top e.CellBounds.Height); Point[] ps = new Point[] { p1, p2, p3 }; e.Graphics.DrawLines(gridLinePen, ps); //畫圖標 e.Graphics.DrawImage(img, newRect); //畫字符串 e.Graphics.DrawString(intGrade.ToString(), e.CellStyle.Font, Brushes.Crimson, e.CellBounds.Left 20, e.CellBounds.Top 5, StringFormat.GenericDefault); e.Handled = true; } } } } } }
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
namespace?TestDataGridViewRowstyle
{
????public?partial?class?Form1?:?Form
????{
????????//定義兩種行樣式
????????private?DataGridViewCellstyle?m_RowstyleNormal;
????????private?DataGridViewCellstyle?m_RowstyleAlternate;
????????//成績單DataTable
????????private?DataTable?m_GradeTable;
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????this.dgvGrade.AutoGenerateColumns?=?false;
????????????this.SetRowstyle();
????????????this.BindData();
????????}
????????///?
????????///?設置行樣式
??????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????151??2008-11-04?17:08??TestDataGridViewRowst
?????文件????????961??2008-11-04?14:23??TestDataGridViewRowst
????..A..H.?????15872??2009-04-13?15:12??TestDataGridViewRowst
?????文件????????483??2008-11-04?14:23??TestDataGridViewRowst
?????文件???????4040??2009-04-13?15:01??TestDataGridViewRowst
?????文件????????987??2008-11-04?16:48??TestDataGridViewRowst
?????文件????????869??2008-11-04?16:48??TestDataGridViewRowst
?????文件????????802??2008-11-04?16:48??TestDataGridViewRowst
?????文件????????788??2008-11-04?16:48??TestDataGridViewRowst
?????文件????????779??2008-11-04?16:48??TestDataGridViewRowst
?????文件????????115??2007-11-27?09:08??TestDataGridViewRowst
?????文件?????????53??2007-09-29?00:44??TestDataGridViewRowst
?????文件?????????53??2007-09-29?00:44??TestDataGridViewRowst
?????文件????????275??2007-11-22?14:20??TestDataGridViewRowst
?????文件????????183??2007-11-22?14:20??TestDataGridViewRowst
?????文件???????1272??2008-11-04?16:59??TestDataGridViewRowst
?????文件???????5357??2009-04-13?14:48??TestDataGridViewRowst
?????文件???????1109??2008-11-04?14:23??TestDataGridViewRowst
?????文件????????249??2008-11-04?14:23??TestDataGridViewRowst
?????文件???????8353??2009-04-13?14:48??TestDataGridViewRowst
?????文件??????32768??2009-04-13?15:11??TestDataGridViewRowst
?????文件???????5024??2009-04-13?14:52??TestDataGridViewRowst
?????文件???????6573??2009-04-13?14:52??TestDataGridViewRowst
?????文件??????10258??2009-04-13?15:11??TestDataGridViewRowst
?????目錄??????????0??2009-04-13?14:43??TestDataGridViewRowst
?????目錄??????????0??2009-04-13?14:43??TestDataGridViewRowst
?????目錄??????????0??2009-04-13?14:43??TestDataGridViewRowst
?????目錄??????????0??2009-04-13?14:43??TestDataGridViewRowst
?????目錄??????????0??2009-04-13?14:43??TestDataGridViewRowst
?????目錄??????????0??2009-04-13?14:43??TestDataGridViewRowst
............此處省略3個文件信息
評論
共有 條評論