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

  • 相关阅读:
    luogu 1865 数论 线性素数筛法
    洛谷 2921 记忆化搜索 tarjan 基环外向树
    洛谷 1052 dp 状态压缩
    洛谷 1156 dp
    洛谷 1063 dp 区间dp
    洛谷 2409 dp 月赛题目
    洛谷1199 简单博弈 贪心
    洛谷1417 烹调方案 dp 贪心
    洛谷1387 二维dp 不是特别简略的题解 智商题
    2016 10 28考试 dp 乱搞 树状数组
  • 原文地址:https://www.cnblogs.com/scottckt/p/2087347.html
Copyright © 2011-2022 走看看