zoukankan      html  css  js  c++  java
  • DataGirdView 自定义 进度条列

    我要实现一个带有进度条的列,如下图。

     

     实现代码:

    1、添加引用

    using System.Drawing;
    using System.ComponentModel;

     2、自定义DataGridViewcolumn

        /// <summary>
        
    /// 带进度列。单元格内容格式为:值/总值 Eric
        
    /// </summary>
        [Description("带进度列。单元格内容格式为:值/总值")]
        
    public class DataGridViewProcessColumn : DataGridViewColumn
        {
            
    public DataGridViewProcessColumn()
                : 
    base(new DataGridViewProcessCell())
            {
            }
        }

     3、自定义DataGridViewCell

       /// <summary>
        
    /// 带进度单元格。单元格内容格式为:值/总值 Eric
        
    /// </summary>
        [Description("带进度单元格。单元格内容格式为:值/总值")]
        
    public class DataGridViewProcessCell : DataGridViewCell
        {
            
    /// <summary>
            
    /// 进度条景色
            
    /// </summary>
            protected Color _ProcessColor = Color.FromArgb(64192192);

            
    public DataGridViewProcessCell()
            {

            }
            
    public DataGridViewProcessCell(Color processBackColor)
                : 
    base()
            {
                _ProcessColor 
    = processBackColor;
            }

            
    protected override void Paint(Graphics graphics, Rectangle clipBounds,
                Rectangle cellBounds, 
    int rowIndex, DataGridViewElementStates cellState,
                
    object value, object formattedValue, string errorText,
                DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
                DataGridViewPaintParts paintParts)
            {
                
    using (SolidBrush backBrush = new SolidBrush(cellStyle.BackColor))
                {
                    
    //维制单元格背景
                    graphics.FillRectangle(backBrush, cellBounds);
                }
                
    base.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);

                
    float percent = 0F;
                
    if (value != null)
                {
                    
    string[] resultAndTotal = value.ToString().Split('/');
                    
    if (resultAndTotal.Count() == 2)
                    {
                        
    float result = 0;
                        
    float total = 0;
                        
    if (!float.TryParse(resultAndTotal[0], out result))
                        {
                            percent 
    = 0;
                        }
                        
    else if (!float.TryParse(resultAndTotal[1], out total))
                        {
                            percent 
    = 0;
                        }
                        
    else
                        {
                            percent 
    = result / total;
                        }
                    }
                }
                
    using (SolidBrush foreBrush = new SolidBrush(_ProcessColor))
                {
                    
    //绘制进度图
                    graphics.FillRectangle(foreBrush, cellBounds.X, cellBounds.Y + cellBounds.Height / 4, cellBounds.Width * percent, cellBounds.Height / 2);
                }
                
    using (SolidBrush residueBrush = new SolidBrush(Color.LightGray))
                {
                    
    //绘进度条余下部分
                    graphics.FillRectangle(residueBrush, cellBounds.X + cellBounds.Width * percent, cellBounds.Y + cellBounds.Height / 4, cellBounds.Width * (1 - percent), cellBounds.Height / 2);
                }
                
    string cellText = value.ToString();
                SizeF sf 
    = graphics.MeasureString(cellText, cellStyle.Font);
                
    float x = cellBounds.X + (cellBounds.Width - sf.Width) / 2f;
                
    float y = cellBounds.Y + (cellBounds.Height - sf.Height) / 2f;
                
    //维制单元格文本
                graphics.DrawString(cellText, cellStyle.Font, new SolidBrush(cellStyle.ForeColor), x, y);

            }
        }

     运行:如果某Cell的value为:1/20。则我们看到结果将会与图中最后一条结果相同。

  • 相关阅读:
    06文件操作
    内核层和用户层通讯的三种方式
    中断门
    工业控制网络安全技术与实践
    前言-anton安卓逆向的入门(零)
    前言与目录-白帽子讲Web安全(零)
    前言与目录-IDA Pro权威指南(零)
    前言与目录-C/C++常用算法手册(零)
    前言与目录-java常用算法手册(零)
    《想你的时候我和自己靠得很近》
  • 原文地址:https://www.cnblogs.com/scottckt/p/2087347.html
Copyright © 2011-2022 走看看