在一些项目管理系统里经常要用一个进度条去动态显示当前项目进展情况,那如何在GridView里实现进度条呢?首先要有一个百分比(如根据当前的情况和原先计划的工期计算一个项目完成的百分比),然后根据这个百分比去画一个进度条。
这里要用到.ashx文件,代码如下:
<%@ 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);
}
}
}
//下面是必须要的
public bool IsReusable {
get {
return false;
}
}
}
下面是.aspx中的代码:
src='<%# "ChartHandler.ashx?rateofprogress="+Eval("RateOfProgressVc") %>' />
数据库中最好是有一个字段用来存放计算出来的百分比,然后Eval("")直接取这个值传过去。
本文作者:hakule 来源:http://blog.csdn.net/hakule/archive/2008/11/17/332
CIO之家 www.ciozj.com 微信公众号:imciow