在用友金蝶等财务软件中,经常需要输入货币类型的数据, 那么这种输入框要如何制作呢?
扩展DataGridView 的功能 出自在天空飞翔博客 http://www.cnblogs.com/michaelhuwei/archive/2010/07/07/1772965.html
如果要使用DEV控件XtraGrid实现同样的效果
需要实现 GridView两个事件,CustomDrawCell和CustomDrawFooterCell
效果如下
实现代码如下
绘制单元格货币格式线条
private static void DrawCellLine(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e,DevExpress.XtraGrid.Columns.GridColumn column,
DevExpress.XtraGrid.GridControl gridControl)
{
int P_WIDTH = ;
int fe = ;
string formatStr = string.Empty;
if (e.Column.FieldName == column.FieldName)
{
//获取设置小数位
if (e.Column.ColumnEdit != null)
{
formatStr = (e.Column.ColumnEdit.DisplayFormat as DevExpress.Utils.FormatInfo).GetDisplayText(e.CellValue);
fe = formatStr.Substring(formatStr.IndexOf('.') + ).Length;
}
else
{
formatStr = (e.Column.DisplayFormat as FormatInfo).GetDisplayText(e.CellValue);
fe = formatStr.Substring(formatStr.IndexOf('.') + ).Length;
} //画出10个整数位,2个小数位
for (int i = ; i < (e.Bounds.Width / ) - fe; i++)
{
if (i % == )
{
e.Graphics.DrawLine(Pens.DarkCyan, e.Bounds.Left + i * P_WIDTH, ,
e.Bounds.Left + i * P_WIDTH, gridControl.Height);
}
else
{
e.Graphics.DrawLine(Pens.LightGray, e.Bounds.Left + i * P_WIDTH, ,
e.Bounds.Left + i * P_WIDTH, gridControl.Height);
}
}
e.Graphics.DrawLine(Pens.Red, e.Bounds.Left + ((e.Bounds.Width / ) - fe) * P_WIDTH, , e.Bounds.Left + ((e.Bounds.Width / ) - fe) * P_WIDTH,
gridControl.Height);
if (fe > )
{
for (int j = ; j < fe - ; j++)
{
e.Graphics.DrawLine(Pens.LightGray, e.Bounds.Left + ((e.Bounds.Width / ) - fe++j) * P_WIDTH, , e.Bounds.Left + ((e.Bounds.Width / ) - fe++j) * P_WIDTH,
gridControl.Height);
}
}
//e.Graphics.DrawLine(Pens.DarkCyan,
var sf = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
decimal v = Convert.ToDecimal(e.CellValue);
string s_int = ((int)v).ToString();
//两位小数 string s_dec = formatStr.ToString().Substring(formatStr.ToString().IndexOf('.')+, fe);
string s_value = s_int + s_dec;
for (int i = ; i < s_value.Length; i++)
{
string ch = s_value[s_value.Length - i - ].ToString();
int x = e.Bounds.Left + ((e.Bounds.Width / ) - i - ) * P_WIDTH;
int y = e.Bounds.Top;
var rect = new RectangleF(x, y, P_WIDTH, e.Bounds.Height);
e.Graphics.DrawString(ch, e.Column.AppearanceCell.Font, Brushes.Black, rect, sf);
}
e.Handled = true;
}
}
private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
DrawCellLine(e, colCName,gridControl1);
}
绘制汇总货币格式线条
private static void DrawCellLine(DevExpress.XtraGrid.Views.Grid.FooterCellCustomDrawEventArgs e, DevExpress.XtraGrid.Columns.GridColumn column,
DevExpress.XtraGrid.GridControl gridControl)
{
int P_WIDTH = ;
int fe = ;
string formatStr = string.Empty;
if (e.Column.FieldName == column.FieldName)
{
//获取设置小数位
if (e.Column.ColumnEdit != null)
{
formatStr = (e.Column.ColumnEdit.DisplayFormat as DevExpress.Utils.FormatInfo).GetDisplayText(e.Info.Value);
fe = formatStr.Substring(formatStr.IndexOf('.') + ).Length;
}
else
{
formatStr = (e.Column.DisplayFormat as FormatInfo).GetDisplayText(e.Info.Value);
fe = formatStr.Substring(formatStr.IndexOf('.') + ).Length;
} //画出10个整数位,2个小数位
for (int i = ; i < (e.Bounds.Width / ) - fe; i++)
{
if (i % == )
{
e.Graphics.DrawLine(Pens.DarkCyan, e.Bounds.Left + i * P_WIDTH, ,
e.Bounds.Left + i * P_WIDTH, gridControl.Height);
}
else
{
e.Graphics.DrawLine(Pens.LightGray, e.Bounds.Left + i * P_WIDTH, ,
e.Bounds.Left + i * P_WIDTH, gridControl.Height);
}
}
e.Graphics.DrawLine(Pens.Red, e.Bounds.Left + ((e.Bounds.Width / ) - fe) * P_WIDTH, , e.Bounds.Left + ((e.Bounds.Width / ) - fe) * P_WIDTH,
gridControl.Height);
if (fe > )
{
for (int j = ; j < fe - ; j++)
{
e.Graphics.DrawLine(Pens.LightGray, e.Bounds.Left + ((e.Bounds.Width / ) - fe + + j) * P_WIDTH, , e.Bounds.Left + ((e.Bounds.Width / ) - fe + + j) * P_WIDTH,
gridControl.Height);
}
}
//e.Graphics.DrawLine(Pens.DarkCyan,
var sf = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
decimal v = Convert.ToDecimal(e.Info.Value);
string s_int = ((int)v).ToString();
//两位小数 string s_dec = formatStr.ToString().Substring(formatStr.ToString().IndexOf('.') + , fe);
string s_value = s_int + s_dec;
for (int i = ; i < s_value.Length; i++)
{
string ch = s_value[s_value.Length - i - ].ToString();
int x = e.Bounds.Left + ((e.Bounds.Width / ) - i - ) * P_WIDTH;
int y = e.Bounds.Top;
var rect = new RectangleF(x, y, P_WIDTH, e.Bounds.Height);
e.Graphics.DrawString(ch, e.Column.AppearanceCell.Font, Brushes.Black, rect, sf);
}
e.Handled = true;
}
}
private void gridView1_CustomDrawFooterCell(object sender, FooterCellCustomDrawEventArgs e)
{
DrawCellLine(e, colCName, gridControl1);
}