效果如下:
代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8
using System.Diagnostics;
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
namespace WindowsApplication37
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
12
public class FinanceTextBoxCell : DataGridViewTextBoxCell
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
15
/// 线间隔大小
16
/// </summary>
17
private int m_iLineSpace ;
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
19
/// 线粗
20
/// </summary>
21
private float m_fBold ;
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
24
/// 红线颜色,特殊占位颜色,普通线颜色
25
/// </summary>
26
private Color m_redColor;
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
private Color m_specialColor;
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
private Color m_normalColor;
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
public FinanceTextBoxCell()
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
}
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
43
/// 线间距
44
/// </summary>
45
public int LineSpace
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47
set
48![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
49
m_iLineSpace = value;
50
51
}
52
get
53![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54
FinanceTextBoxColumn financeTextBoxColumn= OwningColumn as FinanceTextBoxColumn;
55
if (financeTextBoxColumn != null)
56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
if (financeTextBoxColumn.LineSpace != 0)
58![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
59
return financeTextBoxColumn.LineSpace;
60
}
61
}
62
return m_iLineSpace;
63
}
64
}
65
public float LineBold
66![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
67
get
68![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
69
FinanceTextBoxColumn financeTextBoxColumn = OwningColumn as FinanceTextBoxColumn;
70
if (financeTextBoxColumn != null)
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
72
if (financeTextBoxColumn.LineBold != 0.0f)
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
return financeTextBoxColumn.LineBold;
75
}
76
}
77
return m_fBold;
78
}
79
set
80![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
81
m_fBold = value;
82
}
83
}
84
public Color RedColor
85![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
86
get
87![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
88
FinanceTextBoxColumn financeTextBoxColumn = OwningColumn as FinanceTextBoxColumn;
89
if (financeTextBoxColumn != null)
90![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
91
if (financeTextBoxColumn.RedColor != Color.Empty)
92![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
93
return financeTextBoxColumn.RedColor;
94
}
95
}
96
return m_redColor;
97
}
98
set
99![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
100
m_redColor = value;
101
}
102
}
103
public Color SpecialColor
104![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
105
get
106![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
107
FinanceTextBoxColumn financeTextBoxColumn = OwningColumn as FinanceTextBoxColumn;
108
if (financeTextBoxColumn != null)
109![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
110
if (financeTextBoxColumn.SpecialColor != Color.Empty)
111![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
112
return financeTextBoxColumn.SpecialColor;
113
}
114
}
115
return m_specialColor;
116
}
117
set
118![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
119
m_specialColor = value;
120
}
121
}
122
public Color NormalColor
123![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
124
get
125![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
126
FinanceTextBoxColumn financeTextBoxColumn = OwningColumn as FinanceTextBoxColumn;
127
if (financeTextBoxColumn != null)
128![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
129
if (financeTextBoxColumn.NormalColor != Color.Empty)
130![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
131
return financeTextBoxColumn.NormalColor;
132
}
133
}
134
return m_normalColor;
135
}
136
set
137![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
147![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
154
return ((paintParts & DataGridViewPaintParts.Border) != DataGridViewPaintParts.None);
155
}
156
private bool PaintSelectionBackground(DataGridViewPaintParts paintParts)
157![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
158
return ((paintParts & DataGridViewPaintParts.SelectionBackground) != DataGridViewPaintParts.None);
159
}
160
private bool PaintBackground(DataGridViewPaintParts paintParts)
161![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
162
return ((paintParts & DataGridViewPaintParts.Background) != DataGridViewPaintParts.None);
163
}
164
private bool PaintFocus(DataGridViewPaintParts paintParts)
165![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
166
return ((paintParts & DataGridViewPaintParts.Focus) != DataGridViewPaintParts.None);
167
}
168
private bool PaintSelected(DataGridViewElementStates cellState)
169![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
170
return (cellState & DataGridViewElementStates.Selected) != DataGridViewElementStates.None;
171
}
172
internal static bool PaintContentForeground(DataGridViewPaintParts paintParts)
173![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
178
SolidBrush solidBrush;
179![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
180
if (paint && PaintBorder(paintParts))
181![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
194
if ((PaintSelectionBackground(paintParts) && isCellSelected) && !isCell)
195![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
196
solidBrush = new SolidBrush(cellStyle.SelectionBackColor);
197
}
198
else
199![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
200
solidBrush = new SolidBrush(cellStyle.BackColor);
201
}
202
if (paint && PaintBackground(paintParts)
203
&& cellBounds.Width > 0
204
&& (cellBounds.Height > 0))
205![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
206
graphics.FillRectangle(solidBrush, cellBounds);
207
}
208
int i = cellBounds.Width;
209
int j = 1;
210![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
211
212
while (i > 0)
213![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
214
if (i - LineSpace > 0)
215![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
216
i = i - LineSpace;
217
218
if (j == 2)
219![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
220
using (Pen redPen = new Pen(RedColor, LineBold))
221![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
228
if (j > 2 && (j - 2) % 3 == 0)
229![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
230
using (Pen specialPen = new Pen(SpecialColor, LineBold))
231![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
238
using (Pen normalPen = new Pen(NormalColor, LineBold))
239![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
257
decimal d=0;
258
Decimal.TryParse(sFormattedValue, out d);
259
sFormattedValue = d.ToString("#0.00");
260
sFormattedValue = "¥" + sFormattedValue;
261
if (!PaintContentForeground(paintParts))
262![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
263
return;
264
}
265
int x = cellBounds.Width;
266
int y = sFormattedValue.Length-1;
267
while (x > 0)
268![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
269
if (y== -1)
270![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
271
break;
272
}
273
if (x - LineSpace > 0)
274![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
275
x = x - LineSpace;
276
if (sFormattedValue[y].ToString() == ".")
277![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
278
y--;
279
}
280
using (SolidBrush brush = new SolidBrush(cellStyle.ForeColor))
281![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
282
graphics.DrawString(sFormattedValue[y].ToString(), cellStyle.Font, brush,
283
cellBounds.Left + x + 2, cellBounds.Top + 2);
284
}
285
}
286
else
287![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
288
x = 0;
289
}
290
y--;
291
}
292
293
}
294
}
295
}
296
}
297![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)