datatable 的DataColumn进度条列
c#吧
全部回复
仅看楼主
level 1
bxs82 楼主
现在做一个东西需要datagridview上有一列是显示进度条,在网上找了很多例子都是关于重写一个类,然后通过Graphics去画一个进度条,但是这种就有个问题是只能在datagridview上添加这种列,我现在是数据datatable 绑定到 datagridview,绑定后没法再给 datagridview添加列了。
有没有什么别的办法呢?
2020年11月19日 02点11分 1
level 1
bxs82 楼主
搞定了,条条大路通罗马,哈
public class DgvProgressBarColumn
{
public int Press { get; set; }
//进度条图片属性
public Image PressImg
{
get
{
//这里给104是为了左边和右边空出2个像素,剩余的100就是百分比的值
Bitmap bmp = new Bitmap(104, 20);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White); //背景填白色
g.FillRectangle(Brushes.LightSteelBlue, 0, 0, this.Press, 26); //进度条颜色
Font font = new Font("微软雅黑", 9);
SolidBrush sbrush = new SolidBrush(Color.Black);
StringFormat format1 = new StringFormat();
//指定字符串的水平对齐方式
format1.Alignment = StringAlignment.Center;
//表示字符串的垂直对齐方式
format1.LineAlignment = StringAlignment.Center;
Rectangle paintRect = new Rectangle(0, 0, 104, 20);
g.DrawString(Press.ToString() + " %", font, sbrush, paintRect, format1);
return bmp;
}
}
}
但类没有重绘事件,如果改变列宽不会跟着重绘,不过不影响什么
然后调用的时候,大概是:
datatable2.Columns.Add("理论生产进度", typeof(Image)); //数据类型为 Image
DataRow dw = datatable2.NewRow();
DgvProgressBarColumn dgvProgres = new DgvProgressBarColumn();
dgvProgres.Press = 58;
dw[1] = dgvProgres.PressImg; //理论生产进度
2020年11月19日 06点11分 3
1