效果如下:
代码如下:
Code
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8using System.Diagnostics;
9
10namespace WindowsApplication37
11{
12 public class FinanceTextBoxCell : DataGridViewTextBoxCell
13 {
14 /**//// <summary>
15 /// 线间隔大小
16 /// </summary>
17 private int m_iLineSpace ;
18 /**//// <summary>
19 /// 线粗
20 /// </summary>
21 private float m_fBold ;
22
23 /**//// <summary>
24 /// 红线颜色,特殊占位颜色,普通线颜色
25 /// </summary>
26 private Color m_redColor;
27
28 private Color m_specialColor;
29
30 private Color m_normalColor;
31
32
33 public FinanceTextBoxCell()
34 {
35 m_iLineSpace = 12;
36 m_fBold = 1.0f;
37 m_redColor = Color.Red;
38 m_specialColor = Color.Blue;
39 m_normalColor = Color.Gray;
40
41 }
42 /**//// <summary>
43 /// 线间距
44 /// </summary>
45 public int LineSpace
46 {
47 set
48 {
49 m_iLineSpace = value;
50
51 }
52 get
53 {
54 FinanceTextBoxColumn financeTextBoxColumn= OwningColumn as FinanceTextBoxColumn;
55 if (financeTextBoxColumn != null)
56 {
57 if (financeTextBoxColumn.LineSpace != 0)
58 {
59 return financeTextBoxColumn.LineSpace;
60 }
61 }
62 return m_iLineSpace;
63 }
64 }
65 public float LineBold
66 {
67 get
68 {
69 FinanceTextBoxColumn financeTextBoxColumn = OwningColumn as FinanceTextBoxColumn;
70 if (financeTextBoxColumn != null)
71 {
72 if (financeTextBoxColumn.LineBold != 0.0f)
73 {
74 return financeTextBoxColumn.LineBold;
75 }
76 }
77 return m_fBold;
78 }
79 set
80 {
81 m_fBold = value;
82 }
83 }
84 public Color RedColor
85 {
86 get
87 {
88 FinanceTextBoxColumn financeTextBoxColumn = OwningColumn as FinanceTextBoxColumn;
89 if (financeTextBoxColumn != null)
90 {
91 if (financeTextBoxColumn.RedColor != Color.Empty)
92 {
93 return financeTextBoxColumn.RedColor;
94 }
95 }
96 return m_redColor;
97 }
98 set
99 {
100 m_redColor = value;
101 }
102 }
103 public Color SpecialColor
104 {
105 get
106 {
107 FinanceTextBoxColumn financeTextBoxColumn = OwningColumn as FinanceTextBoxColumn;
108 if (financeTextBoxColumn != null)
109 {
110 if (financeTextBoxColumn.SpecialColor != Color.Empty)
111 {
112 return financeTextBoxColumn.SpecialColor;
113 }
114 }
115 return m_specialColor;
116 }
117 set
118 {
119 m_specialColor = value;
120 }
121 }
122 public Color NormalColor
123 {
124 get
125 {
126 FinanceTextBoxColumn financeTextBoxColumn = OwningColumn as FinanceTextBoxColumn;
127 if (financeTextBoxColumn != null)
128 {
129 if (financeTextBoxColumn.NormalColor != Color.Empty)
130 {
131 return financeTextBoxColumn.NormalColor;
132 }
133 }
134 return m_normalColor;
135 }
136 set
137 {
138 m_normalColor = value;
139 }
140 }
141 protected override void Paint(Graphics graphics, Rectangle clipBounds,
142 Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,
143 object value, object formattedValue, string errorText,
144 DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
145 DataGridViewPaintParts paintParts)
146 {
147
148 this.PaintPrivate(graphics, clipBounds, cellBounds, rowIndex,
149 cellState, formattedValue, errorText, cellStyle,
150 advancedBorderStyle, paintParts, false, false, true);
151 }
152 private bool PaintBorder(DataGridViewPaintParts paintParts)
153 {
154 return ((paintParts & DataGridViewPaintParts.Border) != DataGridViewPaintParts.None);
155 }
156 private bool PaintSelectionBackground(DataGridViewPaintParts paintParts)
157 {
158 return ((paintParts & DataGridViewPaintParts.SelectionBackground) != DataGridViewPaintParts.None);
159 }
160 private bool PaintBackground(DataGridViewPaintParts paintParts)
161 {
162 return ((paintParts & DataGridViewPaintParts.Background) != DataGridViewPaintParts.None);
163 }
164 private bool PaintFocus(DataGridViewPaintParts paintParts)
165 {
166 return ((paintParts & DataGridViewPaintParts.Focus) != DataGridViewPaintParts.None);
167 }
168 private bool PaintSelected(DataGridViewElementStates cellState)
169 {
170 return (cellState & DataGridViewElementStates.Selected) != DataGridViewElementStates.None;
171 }
172 internal static bool PaintContentForeground(DataGridViewPaintParts paintParts)
173 {
174 return ((paintParts & DataGridViewPaintParts.ContentForeground) != DataGridViewPaintParts.None);
175 }
176 protected void PaintPrivate(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, bool computeContentBounds, bool computeErrorIconBounds, bool paint)
177 {
178 SolidBrush solidBrush;
179
180 if (paint && PaintBorder(paintParts))
181 {
182 this.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
183 }
184 Rectangle advanceRectangle = this.BorderWidths(advancedBorderStyle);
185 cellBounds.Offset(advanceRectangle.X, advanceRectangle.Y);
186 cellBounds.Width -= advanceRectangle.Right;
187 cellBounds.Height -= advanceRectangle.Bottom;
188
189 bool isCell = (DataGridView.CurrentCellAddress.X == base.ColumnIndex)
190 && (DataGridView.CurrentCellAddress.Y == rowIndex)
191 && (DataGridView.EditingControl != null);
192 bool isCellSelected = (cellState & DataGridViewElementStates.Selected) != DataGridViewElementStates.None;
193
194 if ((PaintSelectionBackground(paintParts) && isCellSelected) && !isCell)
195 {
196 solidBrush = new SolidBrush(cellStyle.SelectionBackColor);
197 }
198 else
199 {
200 solidBrush = new SolidBrush(cellStyle.BackColor);
201 }
202 if (paint && PaintBackground(paintParts)
203 && cellBounds.Width > 0
204 && (cellBounds.Height > 0))
205 {
206 graphics.FillRectangle(solidBrush, cellBounds);
207 }
208 int i = cellBounds.Width;
209 int j = 1;
210
211
212 while (i > 0)
213 {
214 if (i - LineSpace > 0)
215 {
216 i = i - LineSpace;
217
218 if (j == 2)
219 {
220 using (Pen redPen = new Pen(RedColor, LineBold))
221 {
222 graphics.DrawLine(redPen, new Point(cellBounds.Left + i, cellBounds.Top),
223 new Point(cellBounds.Left + i, cellBounds.Top + cellBounds.Height));
224 }
225 }
226 else
227 {
228 if (j > 2 && (j - 2) % 3 == 0)
229 {
230 using (Pen specialPen = new Pen(SpecialColor, LineBold))
231 {
232 graphics.DrawLine(specialPen, new Point(cellBounds.Left + i, cellBounds.Top),
233 new Point(cellBounds.Left + i, cellBounds.Top + cellBounds.Height));
234 }
235 }
236 else
237 {
238 using (Pen normalPen = new Pen(NormalColor, LineBold))
239 {
240 graphics.DrawLine(normalPen, new Point(cellBounds.Left + i, cellBounds.Top),
241 new Point(cellBounds.Left + i, cellBounds.Top + cellBounds.Height));
242 }
243 }
244 }
245 }
246 else
247 {
248 i = 0;
249 }
250 j++;
251 }
252 // Rectangle rec=
253 //写值
254 string sFormattedValue = formattedValue as string;
255 if (!String.IsNullOrEmpty(sFormattedValue) && ((paint && !isCell) || computeContentBounds))
256 {
257 decimal d=0;
258 Decimal.TryParse(sFormattedValue, out d);
259 sFormattedValue = d.ToString("#0.00");
260 sFormattedValue = "¥" + sFormattedValue;
261 if (!PaintContentForeground(paintParts))
262 {
263 return;
264 }
265 int x = cellBounds.Width;
266 int y = sFormattedValue.Length-1;
267 while (x > 0)
268 {
269 if (y== -1)
270 {
271 break;
272 }
273 if (x - LineSpace > 0)
274 {
275 x = x - LineSpace;
276 if (sFormattedValue[y].ToString() == ".")
277 {
278 y--;
279 }
280 using (SolidBrush brush = new SolidBrush(cellStyle.ForeColor))
281 {
282 graphics.DrawString(sFormattedValue[y].ToString(), cellStyle.Font, brush,
283 cellBounds.Left + x + 2, cellBounds.Top + 2);
284 }
285 }
286 else
287 {
288 x = 0;
289 }
290 y--;
291 }
292
293 }
294 }
295 }
296}
297
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8using System.Diagnostics;
9
10namespace WindowsApplication37
11{
12 public class FinanceTextBoxCell : DataGridViewTextBoxCell
13 {
14 /**//// <summary>
15 /// 线间隔大小
16 /// </summary>
17 private int m_iLineSpace ;
18 /**//// <summary>
19 /// 线粗
20 /// </summary>
21 private float m_fBold ;
22
23 /**//// <summary>
24 /// 红线颜色,特殊占位颜色,普通线颜色
25 /// </summary>
26 private Color m_redColor;
27
28 private Color m_specialColor;
29
30 private Color m_normalColor;
31
32
33 public FinanceTextBoxCell()
34 {
35 m_iLineSpace = 12;
36 m_fBold = 1.0f;
37 m_redColor = Color.Red;
38 m_specialColor = Color.Blue;
39 m_normalColor = Color.Gray;
40
41 }
42 /**//// <summary>
43 /// 线间距
44 /// </summary>
45 public int LineSpace
46 {
47 set
48 {
49 m_iLineSpace = value;
50
51 }
52 get
53 {
54 FinanceTextBoxColumn financeTextBoxColumn= OwningColumn as FinanceTextBoxColumn;
55 if (financeTextBoxColumn != null)
56 {
57 if (financeTextBoxColumn.LineSpace != 0)
58 {
59 return financeTextBoxColumn.LineSpace;
60 }
61 }
62 return m_iLineSpace;
63 }
64 }
65 public float LineBold
66 {
67 get
68 {
69 FinanceTextBoxColumn financeTextBoxColumn = OwningColumn as FinanceTextBoxColumn;
70 if (financeTextBoxColumn != null)
71 {
72 if (financeTextBoxColumn.LineBold != 0.0f)
73 {
74 return financeTextBoxColumn.LineBold;
75 }
76 }
77 return m_fBold;
78 }
79 set
80 {
81 m_fBold = value;
82 }
83 }
84 public Color RedColor
85 {
86 get
87 {
88 FinanceTextBoxColumn financeTextBoxColumn = OwningColumn as FinanceTextBoxColumn;
89 if (financeTextBoxColumn != null)
90 {
91 if (financeTextBoxColumn.RedColor != Color.Empty)
92 {
93 return financeTextBoxColumn.RedColor;
94 }
95 }
96 return m_redColor;
97 }
98 set
99 {
100 m_redColor = value;
101 }
102 }
103 public Color SpecialColor
104 {
105 get
106 {
107 FinanceTextBoxColumn financeTextBoxColumn = OwningColumn as FinanceTextBoxColumn;
108 if (financeTextBoxColumn != null)
109 {
110 if (financeTextBoxColumn.SpecialColor != Color.Empty)
111 {
112 return financeTextBoxColumn.SpecialColor;
113 }
114 }
115 return m_specialColor;
116 }
117 set
118 {
119 m_specialColor = value;
120 }
121 }
122 public Color NormalColor
123 {
124 get
125 {
126 FinanceTextBoxColumn financeTextBoxColumn = OwningColumn as FinanceTextBoxColumn;
127 if (financeTextBoxColumn != null)
128 {
129 if (financeTextBoxColumn.NormalColor != Color.Empty)
130 {
131 return financeTextBoxColumn.NormalColor;
132 }
133 }
134 return m_normalColor;
135 }
136 set
137 {
138 m_normalColor = value;
139 }
140 }
141 protected override void Paint(Graphics graphics, Rectangle clipBounds,
142 Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,
143 object value, object formattedValue, string errorText,
144 DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
145 DataGridViewPaintParts paintParts)
146 {
147
148 this.PaintPrivate(graphics, clipBounds, cellBounds, rowIndex,
149 cellState, formattedValue, errorText, cellStyle,
150 advancedBorderStyle, paintParts, false, false, true);
151 }
152 private bool PaintBorder(DataGridViewPaintParts paintParts)
153 {
154 return ((paintParts & DataGridViewPaintParts.Border) != DataGridViewPaintParts.None);
155 }
156 private bool PaintSelectionBackground(DataGridViewPaintParts paintParts)
157 {
158 return ((paintParts & DataGridViewPaintParts.SelectionBackground) != DataGridViewPaintParts.None);
159 }
160 private bool PaintBackground(DataGridViewPaintParts paintParts)
161 {
162 return ((paintParts & DataGridViewPaintParts.Background) != DataGridViewPaintParts.None);
163 }
164 private bool PaintFocus(DataGridViewPaintParts paintParts)
165 {
166 return ((paintParts & DataGridViewPaintParts.Focus) != DataGridViewPaintParts.None);
167 }
168 private bool PaintSelected(DataGridViewElementStates cellState)
169 {
170 return (cellState & DataGridViewElementStates.Selected) != DataGridViewElementStates.None;
171 }
172 internal static bool PaintContentForeground(DataGridViewPaintParts paintParts)
173 {
174 return ((paintParts & DataGridViewPaintParts.ContentForeground) != DataGridViewPaintParts.None);
175 }
176 protected void PaintPrivate(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, bool computeContentBounds, bool computeErrorIconBounds, bool paint)
177 {
178 SolidBrush solidBrush;
179
180 if (paint && PaintBorder(paintParts))
181 {
182 this.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
183 }
184 Rectangle advanceRectangle = this.BorderWidths(advancedBorderStyle);
185 cellBounds.Offset(advanceRectangle.X, advanceRectangle.Y);
186 cellBounds.Width -= advanceRectangle.Right;
187 cellBounds.Height -= advanceRectangle.Bottom;
188
189 bool isCell = (DataGridView.CurrentCellAddress.X == base.ColumnIndex)
190 && (DataGridView.CurrentCellAddress.Y == rowIndex)
191 && (DataGridView.EditingControl != null);
192 bool isCellSelected = (cellState & DataGridViewElementStates.Selected) != DataGridViewElementStates.None;
193
194 if ((PaintSelectionBackground(paintParts) && isCellSelected) && !isCell)
195 {
196 solidBrush = new SolidBrush(cellStyle.SelectionBackColor);
197 }
198 else
199 {
200 solidBrush = new SolidBrush(cellStyle.BackColor);
201 }
202 if (paint && PaintBackground(paintParts)
203 && cellBounds.Width > 0
204 && (cellBounds.Height > 0))
205 {
206 graphics.FillRectangle(solidBrush, cellBounds);
207 }
208 int i = cellBounds.Width;
209 int j = 1;
210
211
212 while (i > 0)
213 {
214 if (i - LineSpace > 0)
215 {
216 i = i - LineSpace;
217
218 if (j == 2)
219 {
220 using (Pen redPen = new Pen(RedColor, LineBold))
221 {
222 graphics.DrawLine(redPen, new Point(cellBounds.Left + i, cellBounds.Top),
223 new Point(cellBounds.Left + i, cellBounds.Top + cellBounds.Height));
224 }
225 }
226 else
227 {
228 if (j > 2 && (j - 2) % 3 == 0)
229 {
230 using (Pen specialPen = new Pen(SpecialColor, LineBold))
231 {
232 graphics.DrawLine(specialPen, new Point(cellBounds.Left + i, cellBounds.Top),
233 new Point(cellBounds.Left + i, cellBounds.Top + cellBounds.Height));
234 }
235 }
236 else
237 {
238 using (Pen normalPen = new Pen(NormalColor, LineBold))
239 {
240 graphics.DrawLine(normalPen, new Point(cellBounds.Left + i, cellBounds.Top),
241 new Point(cellBounds.Left + i, cellBounds.Top + cellBounds.Height));
242 }
243 }
244 }
245 }
246 else
247 {
248 i = 0;
249 }
250 j++;
251 }
252 // Rectangle rec=
253 //写值
254 string sFormattedValue = formattedValue as string;
255 if (!String.IsNullOrEmpty(sFormattedValue) && ((paint && !isCell) || computeContentBounds))
256 {
257 decimal d=0;
258 Decimal.TryParse(sFormattedValue, out d);
259 sFormattedValue = d.ToString("#0.00");
260 sFormattedValue = "¥" + sFormattedValue;
261 if (!PaintContentForeground(paintParts))
262 {
263 return;
264 }
265 int x = cellBounds.Width;
266 int y = sFormattedValue.Length-1;
267 while (x > 0)
268 {
269 if (y== -1)
270 {
271 break;
272 }
273 if (x - LineSpace > 0)
274 {
275 x = x - LineSpace;
276 if (sFormattedValue[y].ToString() == ".")
277 {
278 y--;
279 }
280 using (SolidBrush brush = new SolidBrush(cellStyle.ForeColor))
281 {
282 graphics.DrawString(sFormattedValue[y].ToString(), cellStyle.Font, brush,
283 cellBounds.Left + x + 2, cellBounds.Top + 2);
284 }
285 }
286 else
287 {
288 x = 0;
289 }
290 y--;
291 }
292
293 }
294 }
295 }
296}
297