=90){RowIcon=TestDataGridViewRowStyle.Properties.Resources.GradeA;//从资源文件中获取图片strToolTip="GradeA";}elseif(intGrade>=80){RowIcon=TestDataGridViewRowStyle.Properties.Resources.GradeB;strToolTip="GradeB";}elseif(intGrade>=70){RowIcon=TestDataGridViewRowStyle.Properties.Resources.GradeC;strToolTip="GradeC";}elseif(intGrade>=60){RowIcon=TestDataGridViewRowStyle.Properties.Resources.GradeD;strToolTip="GradeD";}else{RowIcon=TestDataGridViewRowStyle.Properties.Resources.GradeF;strToolTip="GradeF";}e.Graphics.DrawImage(RowIcon,e.RowBounds.Leftthis.dgvGrade.RowHeadersWidth-20,e.RowBounds.Top4,16,16);//绘制图标this.dgvGrade.Rows[e.RowIndex].HeaderCell.ToolTipText=strToolTip;//设置提示信息}privatevoiddgvGrade_CellPainting(objectsender,DataGridViewCellPaintingEventArgse){if(e.RowIndex>=0&&e.ColumnIndex==2){if(this.dgvGrade.Rows[e.RowIndex].Cells["ColumnGrade"].Value==DBNull.Value)return;intintGrade=Convert.ToInt32(this.dgvGrade.Rows[e.RowIndex].Cells["ColumnGrade"].Value);Imageimg;if(intGrade>=90){img=TestDataGridViewRowStyle.Properties.Resources.high;}elseif(intGrade>=80){img=TestDataGridViewRowStyle.Properties.Resources.arrow;}elseif(intGrade>=70){img=TestDataGridViewRowStyle.Properties.Resources.up;}elseif(intGrade>=60){img=TestDataGridViewRowStyle.Properties.Resources.down;}else{img=TestDataGridViewRowStyle.Properties.Resources.low;}RectanglenewRect=newRectangle(e.CellBounds.X3,e.CellBounds.Y5,e.CellBounds.Height-15,e.CellBounds.Height-12);using(BrushgridBrush=newSolidBrush(this.dgvGrade.GridColor),backColorBrush=newSolidBrush(e.CellStyle.BackColor)){using(PengridLinePen=newPen(gridBrush,2)){//Erasethecell.e.Graphics.FillRectangle(backColorBrush,e.CellBounds);//划线Pointp1=newPoint(e.CellBounds.Lefte.CellBounds.Width,e.CellBounds.Top);Pointp2=newPoint(e.CellBounds.Lefte.CellBounds.Width,e.CellBounds.Tope.CellBounds.Height);Pointp3=newPoint(e.CellBounds.Left,e.CellBounds.Tope.CellBounds.Height);Point[]ps=newPoint[]{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.Left20,e.CellBounds.Top5,StringFormat.GenericDefault);e.Handled=true;}}}}}}-IMDN开发者社群-imdn.cn"> =90){RowIcon=TestDataGridViewRowStyle.Properties.Resources.GradeA;//从资源文件中获取图片strToolTip="GradeA";}elseif(intGrade>=80){RowIcon=TestDataGridViewRowStyle.Properties.Resources.GradeB;strToolTip="GradeB";}elseif(intGrade>=70){RowIcon=TestDataGridViewRowStyle.Properties.Resources.GradeC;strToolTip="GradeC";}elseif(intGrade>=60){RowIcon=TestDataGridViewRowStyle.Properties.Resources.GradeD;strToolTip="GradeD";}else{RowIcon=TestDataGridViewRowStyle.Properties.Resources.GradeF;strToolTip="GradeF";}e.Graphics.DrawImage(RowIcon,e.RowBounds.Leftthis.dgvGrade.RowHeadersWidth-20,e.RowBounds.Top4,16,16);//绘制图标this.dgvGrade.Rows[e.RowIndex].HeaderCell.ToolTipText=strToolTip;//设置提示信息}privatevoiddgvGrade_CellPainting(objectsender,DataGridViewCellPaintingEventArgse){if(e.RowIndex>=0&&e.ColumnIndex==2){if(this.dgvGrade.Rows[e.RowIndex].Cells["ColumnGrade"].Value==DBNull.Value)return;intintGrade=Convert.ToInt32(this.dgvGrade.Rows[e.RowIndex].Cells["ColumnGrade"].Value);Imageimg;if(intGrade>=90){img=TestDataGridViewRowStyle.Properties.Resources.high;}elseif(intGrade>=80){img=TestDataGridViewRowStyle.Properties.Resources.arrow;}elseif(intGrade>=70){img=TestDataGridViewRowStyle.Properties.Resources.up;}elseif(intGrade>=60){img=TestDataGridViewRowStyle.Properties.Resources.down;}else{img=TestDataGridViewRowStyle.Properties.Resources.low;}RectanglenewRect=newRectangle(e.CellBounds.X3,e.CellBounds.Y5,e.CellBounds.Height-15,e.CellBounds.Height-12);using(BrushgridBrush=newSolidBrush(this.dgvGrade.GridColor),backColorBrush=newSolidBrush(e.CellStyle.BackColor)){using(PengridLinePen=newPen(gridBrush,2)){//Erasethecell.e.Graphics.FillRectangle(backColorBrush,e.CellBounds);//划线Pointp1=newPoint(e.CellBounds.Lefte.CellBounds.Width,e.CellBounds.Top);Pointp2=newPoint(e.CellBounds.Lefte.CellBounds.Width,e.CellBounds.Tope.CellBounds.Height);Pointp3=newPoint(e.CellBounds.Left,e.CellBounds.Tope.CellBounds.Height);Point[]ps=newPoint[]{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.Left20,e.CellBounds.Top5,StringFormat.GenericDefault);e.Handled=true;}}}}}} - IMDN开发者社群-imdn.cn">
登录
首页 » C# » winform DataGridViewRowStyle 常见用法实例源码下载 有图

winform DataGridViewRowStyle 常见用法实例源码下载 有图

于 2014-08-29 发布
0 87
下载积分: 1 下载次数: 0

代码说明:

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(); } /// /// 设置行样式 /// 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; } /// /// 绑定数据 /// 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; } } } } }}

下载说明:请别用迅雷下载,失败请重下,重下不扣分!

发表评论

0 个回复

  • c#RS485通讯案例 串口调试工具下载
    非常好的c#RS485通讯案例
    2014-07-27下载
    积分:1
  • C#实现串口上位机
    C# 上位机 串口通讯 
    2021-05-06下载
    积分:1
  • devexpress 分页实例源码下载
    devexpress 分页实例源码下载
    2014-01-26下载
    积分:1
  • ASP.NET微信公共平台/微信回复源码下载
    ASP.NET微信公共平台
    2014-04-17下载
    积分:1
  • C#简体繁体转换(附类库)
    实例截图 核心代码
    2021-09-10 00:30:58下载
    积分:1
  • asp.net 实现qq表情功能 附完整源码
    asp.net qq表情功能实现
    2013-09-22下载
    积分:1
  • asp.net防刷新(F5) 禁止重复提交 实例(附完整源码)
    实现了用户操作时 刷新导致的重复操作问题。 实现原理如下:   使用一个上下文唯一的票据号,该票据号随页面提交或回发而递增,每当用户与服务器端交互时,代码会提取上次的票据号码并与当前票据号码比较,如果当前票据大于上次的票据,则是提交或回发,否则则识别为刷新。     这种方式会用到HttpMoudel,并在其中操作Session。   使用方法如下:   1、引入dll MsdnExt 压缩包中有2.修改web.config,在 添加以下item:               其中“MsdnModule”是一个名字, “Msdn.RefreshModule”是“Msdn”下的“RefreshModule”类型,“MsdnExt”是程序集的名字 3.引入命名空间 using Msdn; 并修改页面类继承Msdn.Page 4.使用代码  代码:  protected void Button1_Click(object sender, EventArgs e) { if (!IsPageRefresh) Response.Write("按钮事件"); else Response.Write("页面刷新");TrackRefreshState();} MSDNPage.cs 核心代码如下:using System;using System.Web.UI;using System.Web;using System.Text;namespace Msdn{ public class Page : System.Web.UI.Page { #region Constants // *********************************************************** // Constants public const string RefreshTicketCounter = "RefreshTicketCounter"; private const string SetFocusFunctionName = "__setFocus"; private const string SetFocusScriptName = "__inputFocusHandler"; // *********************************************************** #endregion // *********************************************************** // Ctor public Page() { m_focusedControl = ""; // Register a PreRender handler this.PreRender = new EventHandler(RefreshPage_PreRender); } // *********************************************************** // ************************************************************** // Indicates if the page is being viewed in response to F5 hit public bool IsPageRefresh { get { object o = HttpContext.Current.Items[RefreshAction.PageRefreshEntry]; if (o == null) return false; return (bool) o; } } // ************************************************************** // ************************************************************** // Increase the internal counter used to generate refresh tickets public void TrackRefreshState() { InitRefreshState(); int ticket = Convert.ToInt32(Session[RefreshTicketCounter]) 1; Session[RefreshTicketCounter] = ticket; } // ************************************************************** // ************************************************************** // Set the control with the input focus public void SetFocus(string ctlId) { m_focusedControl = ctlId; } // ************************************************************** #region Private Members // ************************************************************** // Create the hidden field to store the current request ticket private void SaveRefreshState() { int ticket = Convert.ToInt32(Session[RefreshTicketCounter]) 1; RegisterHiddenField(RefreshAction.CurrentRefreshTicketEntry, ticket.ToString()); } // ************************************************************** // ************************************************************** // Ensure that the ticket counter is initialized private void InitRefreshState() { if (Session[RefreshTicketCounter] == null) Session[RefreshTicketCounter] = 0; } // ************************************************************** // ************************************************************** // Handle the PreRender event private void RefreshPage_PreRender(object sender, EventArgs e) { SaveRefreshState(); AddSetFocusScript(); } // ************************************************************** // ************************************************************** // Add any script code required for the SetFocus feature private void AddSetFocusScript() { if (m_focusedControl == "") return; // Add the script to declare the function // (Only one form in ASP.NET pages) StringBuilder sb = new StringBuilder(""); sb.Append(""); sb.Append("function "); sb.Append(SetFocusFunctionName); sb.Append("(ctl) {"); sb.Append(" if (document.forms[0][ctl] != null)"); sb.Append(" {document.forms[0][ctl].focus();}"); sb.Append("}"); // Add the script to call the function sb.Append(SetFocusFunctionName); sb.Append("(); sb.Append(m_focusedControl); sb.Append();"); // Register the script (names are CASE-SENSITIVE) if (!IsStartupScriptRegistered(SetFocusScriptName)) RegisterStartupScript(SetFocusScriptName, sb.ToString()); } // ************************************************************** #endregion #region Private Properties // *********************************************************** // Private properties private string m_focusedControl; // *********************************************************** #endregion }}  
    2021-03-16 17:39:21下载
    积分:1
  • 类似系统文件拖拽的半透明效果
     c#拖拽的时候鼠标效果是很丑陋的,使用这个DEMO,能够实现与拖拽系统文件的一样的半透明效果。代码很详细。
    2014-12-02下载
    积分:1
  • C# 打印dataGridView内容 例子源码
    打印dataGridView内容
    2014-09-17下载
    积分:1
  • c# 加密类 示例代码
    加密算法 全数卷帘加密解密,MD5加密,DES加密解密
    2015-01-19下载
    积分:1
  • 696518资源总数
  • 104269会员总数
  • 31今日下载