2015-02-08 76 views
0

我需要将DevExpress网格单元中的值的百分比表示为图。我可以使用DrawLine进行绘制,但是我的问题是一旦百分比值等于大于1,它将在此代码中被视为100%。请找到下面的代码,如截图所示,3.59应该显示小于8.35!请帮忙。DevExpress网格单元中的值图的百分比

private void CustomDrawCell(object sender, RowCellCustomDrawEventArgs args) 
{ 
    args.Appearance.DrawBackground(args.Graphics, args.Cache, args.Bounds); 
     if (column != null) 
     { 
     int penSize = args.Bounds.Height * 2/3; 
     double value = GetValue(); // This is the value against which I have to display the graph, its in %. 

     int left = args.Bounds.Left; 
     int middle = args.Bounds.Height/2 + args.Bounds.Top; 
     int width = args.Bounds.Width; 

     int right = (int)(left + width * value); 
     args.Graphics.DrawLine(new Pen(Color.Green, penSize), left, middle, right, middle); 

     }   
    args.Handled = true; 
    } 

enter image description here

+0

好像'right'应该是:int right =(int)(width *(value/100))。绘制线条的宽度应该相对于单元格宽度的宽度。 – 2015-02-08 23:32:42

回答

0
int right = (int)(left + width * value); 

此代码计算栏长度正确地仅当是0和1之间。如果值是0到100之间或在0和10之间,则需要相应地乘以100或10的结果。

int right = (int)(left + width * value/100); 

顺便说一下,由于您使用的是XtraGrid,因此无需对自定义绘图进行拼图。有RepositoryItemProgressBar组件,它可以嵌入到XtraGrid单元中。它根据单元格值显示线条,并允许您定义最大值和最小值,以便线条最准确地显示单元格值。阅读本文以了解如何将编辑器分配到列:Assigning Editors to Columns and Card Fields