利用DEVexpress的GridControl添加進度條
第一步:在一個窗體中添加一個GridControl控件,如圖所示:
第二步:點擊控件中的Run Designer,找到Columns,然後再選擇要設置進度條的列,設置屬性如下:
第三步:點擊View,找到事件CustomDrawCell,寫如下的代碼:
private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
if (e.RowHandle == gridView1.FocusedRowHandle)
{
e.Appearance.BackColor = Color.CadetBlue;
}
if (e.Column.FieldName == "ZBJD")//為ZBJD這列設置進度條
{
DrProgressBar(e);
e.Handled = true;
DrawEditor(e);
}
}
private void DrProgressBar(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
decimal percent = Convert.ToDecimal(e.CellValue);
int iIndex = 0;
int width = 0;
if (percent < 0.0000001m)
{
iIndex = 1;
}
if (iIndex == 1)
{
width = (int)(100 * Math.Abs(1) * e.Bounds.Width / 100);
}
else
{
width = (int)(100 * Math.Abs(percent) * e.Bounds.Width / 100);
}
Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, width, e.Bounds.Height);
Brush b = Brushes.Green;
if (percent < 1)
{
b = Brushes.Red;
}
else
{
b = Brushes.Green;
}
e.Graphics.FillRectangle(b, rect);
}
private void DrawEditor(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
GridCellInfo cell = e.Cell as GridCellInfo;
Point offset = cell.CellValueRect.Location;
BaseEditPainter pb = cell.ViewInfo.Painter as BaseEditPainter;
AppearanceObject style = cell.ViewInfo.PaintAppearance;
if (!offset.IsEmpty)
cell.ViewInfo.Offset(offset.X, offset.Y);
try
{
pb.Draw(new ControlGraphicsInfoArgs(cell.ViewInfo, e.Cache, cell.Bounds));
}
finally
{
if (!offset.IsEmpty)
{
cell.ViewInfo.Offset(-offset.X, -offset.Y);
}
}
}
最後效果如下:
註意事項:當你的百分比顯示不出來的時候,設置進度條這列的ColumnEdit一定不要像其它筆者一樣設置一個ProgressBar進度條。當初自己就按照其它筆者的來做,數值總是不顯示,一直困擾了我好多天,所以在這裏提醒大家一下。在下黎希,以後我會常在博客裏寫入有關ArcGIS二次開發的內容,希望和大家一起交流學習!
利用DEVexpress的GridControl添加進度條