• 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。则我们看到结果将会与图中最后一条结果相同。

  • 相关阅读:
    ZABBIX监控TCP连接状态
    MySQL索引(九)
    MySQL字符集、information_schema元数据(八)
    DML(数据库操作语言)(六)
    DDL(数据库定义语言)(五)
    MySQL多实例安装、配置、启动(四)
    CentOS7 编译安装MySQL5.6.38(一)
    MySQL权限管理、配置文件(三)
    MySQL服务的构成(二)
    yum lockfile is held by another process
  • 原文地址:https://www.cnblogs.com/scottckt/p/2087347.html
Copyright © 2020-2023  润新知