DEV 财务货币格式单元格

在用友金蝶等财务软件中,经常需要输入货币类型的数据, 那么这种输入框要如何制作呢?

扩展DataGridView 的功能  出自在天空飞翔博客 http://www.cnblogs.com/michaelhuwei/archive/2010/07/07/1772965.html

如果要使用DEV控件XtraGrid实现同样的效果

需要实现 GridView两个事件,CustomDrawCell和CustomDrawFooterCell

效果如下

DEV 财务货币格式单元格

实现代码如下

绘制单元格货币格式线条

private static void DrawCellLine(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e,DevExpress.XtraGrid.Columns.GridColumn column,

            DevExpress.XtraGrid.GridControl gridControl)

        {

            int P_WIDTH = 10;

            int fe = 2;

            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('.') + 1).Length;

                }

                else

                {

                    formatStr = (e.Column.DisplayFormat as FormatInfo).GetDisplayText(e.CellValue);

                    fe = formatStr.Substring(formatStr.IndexOf('.') + 1).Length;

                }



                //画出10个整数位,2个小数位

                for (int i = 1; i < (e.Bounds.Width / 10) - fe; i++)

                {

                    if (i % 3 == 0)

                    {

                        e.Graphics.DrawLine(Pens.DarkCyan, e.Bounds.Left + i * P_WIDTH, 0,

                                          e.Bounds.Left + i * P_WIDTH, gridControl.Height);

                    }

                    else

                    {

                        e.Graphics.DrawLine(Pens.LightGray, e.Bounds.Left + i * P_WIDTH, 0,

                                          e.Bounds.Left + i * P_WIDTH, gridControl.Height);

                    }

                }

                e.Graphics.DrawLine(Pens.Red, e.Bounds.Left + ((e.Bounds.Width / 10) - fe) * P_WIDTH, 0, e.Bounds.Left + ((e.Bounds.Width / 10) - fe) * P_WIDTH,

                                 gridControl.Height);

                if (fe > 1)

                {

                    for (int j = 0; j < fe - 1; j++)

                    {

                        e.Graphics.DrawLine(Pens.LightGray, e.Bounds.Left + ((e.Bounds.Width / 10) - fe+1+j) * P_WIDTH, 0, e.Bounds.Left + ((e.Bounds.Width / 10) - fe+1+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('.')+1, fe);

                string s_value = s_int + s_dec;

                for (int i = 0; i < s_value.Length; i++)

                {

                    string ch = s_value[s_value.Length - i - 1].ToString();

                    int x = e.Bounds.Left + ((e.Bounds.Width / 10) - i - 1) * 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 = 10;

            int fe = 2;

            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('.') + 1).Length;

                }

                else

                {

                    formatStr = (e.Column.DisplayFormat as FormatInfo).GetDisplayText(e.Info.Value);

                    fe = formatStr.Substring(formatStr.IndexOf('.') + 1).Length;

                }



                //画出10个整数位,2个小数位

                for (int i = 1; i < (e.Bounds.Width / 10) - fe; i++)

                {

                    if (i % 3 == 0)

                    {

                        e.Graphics.DrawLine(Pens.DarkCyan, e.Bounds.Left + i * P_WIDTH, 0,

                                          e.Bounds.Left + i * P_WIDTH, gridControl.Height);

                    }

                    else

                    {

                        e.Graphics.DrawLine(Pens.LightGray, e.Bounds.Left + i * P_WIDTH, 0,

                                          e.Bounds.Left + i * P_WIDTH, gridControl.Height);

                    }

                }

                e.Graphics.DrawLine(Pens.Red, e.Bounds.Left + ((e.Bounds.Width / 10) - fe) * P_WIDTH, 0, e.Bounds.Left + ((e.Bounds.Width / 10) - fe) * P_WIDTH,

                                 gridControl.Height);

                if (fe > 1)

                {

                    for (int j = 0; j < fe - 1; j++)

                    {

                        e.Graphics.DrawLine(Pens.LightGray, e.Bounds.Left + ((e.Bounds.Width / 10) - fe + 1 + j) * P_WIDTH, 0, e.Bounds.Left + ((e.Bounds.Width / 10) - fe + 1 + 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('.') + 1, fe);

                string s_value = s_int + s_dec;

                for (int i = 0; i < s_value.Length; i++)

                {

                    string ch = s_value[s_value.Length - i - 1].ToString();

                    int x = e.Bounds.Left + ((e.Bounds.Width / 10) - i - 1) * 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);

 } 

 

 

你可能感兴趣的:(格式)