在一些项目管理系统里经常要用一个进度条去动态显示当前项目进展情况,那如何在GridView里实现进度条呢?首先要有一个百分比(如根据当前的情况和原先计划的工期计算一个项目完成的百分比),然后根据这个百分比去画一个进度条。
这里要用到.ashx文件,代码如下:# <%@ WebHandler Language="C#" Class="ChartHandler" %>
#
# <%@ WebHandler Language="C#" Class="ChartHandler" %>
#
using System;
# using System.Web;
# using System.Drawing;
# using System.Drawing.Drawing2D;
# using System.Drawing.Imaging;
#
# public class ChartHandler : IHttpHandler {
#
# public void ProcessRequest (HttpContext context) {
# //这个是接收传过来的百分比
# decimal rateofprogress = decimal.Parse(context.Request.QueryString["rateofprogress"]);
#
# using (Bitmap bitmap = new Bitmap(100, 16))
# {
# using (Graphics graphics = Graphics.FromImage(bitmap))
# {
# graphics.SmoothingMode = SmoothingMode.AntiAlias;
# graphics.Clear(Color.White);
# Rectangle rect = new Rectangle(0, 0, 100, 16);
# graphics.DrawRectangle(Pens.Black, rect);
# int width = Convert.ToInt32(rateofprogress);
#
# Rectangle fillRect = new Rectangle(0, 0, width, 16);
# if (width == 100)
# {
# graphics.FillRectangle(Brushes.Green, fillRect);//如果百分比为一百,进度条用绿色显示,表示已完成
# }
# else if(width==0)
# {
# graphics.Clear(Color.Gray);
# graphics.FillRectangle(Brushes.Gray, fillRect);//百分比为0,进度条用灰色显示,表示未开始
# }
# else
# {
# graphics.FillRectangle(Brushes.Blue, fillRect);//正在进行的,用蓝色显示
# }
# context.Response.ContentType = "text/gif";
# context.Response.Clear();
# context.Response.BufferOutput = true;
# bitmap.Save(context.Response.OutputStream, ImageFormat.Gif);
# }
# }
#
# }