001.
Imports
System.ComponentModel
002.
Imports
System.Windows.Forms
003.
Imports
System.Drawing
004.
005.
<ToolboxBitmap(
GetType
(DataGridView))> _
006.
Public
Class
usrCtrGridControlExt :
Inherits
DataGridView
007.
008.
#Region " Enumerations "
009.
Public
Enum
DataGridViewDisplayManager
As
Byte
010.
[
Default
] = 0
011.
Framework = 1
012.
End
Enum
013.
014.
Public
Enum
DataGridViewOnEnterEditModeMove
As
Byte
015.
Right = 0
016.
Down = 1
017.
End
Enum
018.
019.
Public
Enum
DataGridViewOnEnterModeMove
As
Byte
020.
Right = 0
021.
Down = 1
022.
End
Enum
023.
024.
Public
Enum
DataGridViewLastColumnVisible
As
Short
025.
Last = 0
026.
End
Enum
027.
028.
Public
Enum
DataGridViewFirstColumnVisible
As
Short
029.
First = 0
030.
End
Enum
031.
032.
033.
034.
035.
036.
Public
Enum
TextBoxDisplayManager
As
Byte
037.
TextCharacter = 0
038.
TextNumber = 1
039.
TextInteger = 2
040.
End
Enum
041.
042.
Public
Enum
TextBoxDisplayPrecisionScale
As
Byte
043.
None = 0
044.
One = 1
045.
Two = 2
046.
Tree = 3
047.
Four = 4
048.
End
Enum
049.
050.
#End Region
051.
052.
#Region " Variables "
053.
Private
_DisplayManager
As
DataGridViewDisplayManager = DataGridViewDisplayManager.
Default
054.
Private
_OnEnterEditModeMove
As
DataGridViewOnEnterEditModeMove = DataGridViewOnEnterEditModeMove.Right
055.
Private
_OnEnterModeMove
As
DataGridViewOnEnterModeMove = DataGridViewOnEnterModeMove.Right
056.
057.
Private
_OnSortedFindKey
As
String
058.
Private
_CellIsBeginEdit
As
Boolean
=
False
059.
Private
_CellIsBeginEditRow
As
Integer
= -1
060.
Private
_CellIsBeginEditCol
As
UShort
061.
062.
Private
_LastColumnVisible
As
UShort
063.
Private
_FirstColumnVisible
As
UShort
064.
065.
066.
067.
068.
Private
bsGrid
As
New
BindingSource
069.
Private
DTControlGrid
As
New
DataTable(
"DTControlGrid"
)
070.
Private
DTControlGridEnumerable
As
System.Data.EnumerableRowCollection(Of DataRow)
071.
Private
SortPattern
As
New
SortedList
072.
#End Region
073.
074.
#Region " Properties "
075.
076.
<System.ComponentModel.DefaultValue(
GetType
(DataGridViewDisplayManager),
"Default"
), System.ComponentModel.Category(
"Appearance"
), System.ComponentModel.Description(
"กำหนดคุณสมบัติ"
)> _
077.
Public
Property
DisplayManager()
As
DataGridViewDisplayManager
078.
Get
079.
Return
_DisplayManager
080.
End
Get
081.
Set
(
ByVal
value
As
DataGridViewDisplayManager)
082.
_DisplayManager = value
083.
If
Me
._DisplayManager = DataGridViewDisplayManager.Framework
Then
084.
Me
.DisplayManagerEnterEditModeMove = DataGridViewOnEnterEditModeMove.Right
085.
Me
.DisplayManagerEnterModeMove = DataGridViewOnEnterModeMove.Right
086.
Else
087.
088.
Me
.DisplayManagerEnterEditModeMove = DataGridViewOnEnterEditModeMove.Down
089.
Me
.DisplayManagerEnterModeMove = DataGridViewOnEnterModeMove.Down
090.
End
If
091.
End
Set
092.
End
Property
093.
094.
<System.ComponentModel.DefaultValue(
GetType
(DataGridViewOnEnterEditModeMove),
"No"
), System.ComponentModel.Category(
"Appearance"
)> _
095.
Public
Property
DisplayManagerEnterEditModeMove()
As
DataGridViewOnEnterEditModeMove
096.
Get
097.
Return
_OnEnterEditModeMove
098.
End
Get
099.
Set
(
ByVal
Value
As
DataGridViewOnEnterEditModeMove)
100.
_OnEnterEditModeMove = Value
101.
End
Set
102.
End
Property
103.
104.
<System.ComponentModel.DefaultValue(
GetType
(DataGridViewOnEnterEditModeMove),
"First"
), System.ComponentModel.Category(
"Appearance"
)> _
105.
Public
Property
DisplayManagerEnterModeMove()
As
DataGridViewOnEnterModeMove
106.
Get
107.
Return
_OnEnterModeMove
108.
End
Get
109.
Set
(
ByVal
Value
As
DataGridViewOnEnterModeMove)
110.
_OnEnterModeMove = Value
111.
End
Set
112.
End
Property
113.
114.
<System.ComponentModel.DefaultValue(
""
), System.ComponentModel.Category(
"Appearance"
)> _
115.
Public
Property
DisplayManagerSortedFindKey()
As
String
116.
Get
117.
Return
_OnSortedFindKey
118.
End
Get
119.
Set
(
ByVal
value
As
String
)
120.
_OnSortedFindKey = value
121.
End
Set
122.
End
Property
123.
124.
<System.ComponentModel.DefaultValue(0), System.ComponentModel.Category(
"Appearance"
)> _
125.
Public
Property
DisplayManagerFirstColumnVisible()
As
String
126.
Get
127.
Return
_FirstColumnVisible
128.
End
Get
129.
Set
(
ByVal
value
As
String
)
130.
_FirstColumnVisible = value
131.
End
Set
132.
End
Property
133.
134.
<System.ComponentModel.DefaultValue(0), System.ComponentModel.Category(
"Appearance"
)> _
135.
Public
Property
DisplayManagerLastColumnVisible()
As
String
136.
Get
137.
Return
_LastColumnVisible
138.
End
Get
139.
Set
(
ByVal
value
As
String
)
140.
_LastColumnVisible = value
141.
End
Set
142.
End
Property
143.
144.
#End Region
145.
146.
#Region " Constructor "
147.
148.
Public
Sub
New
()
149.
150.
151.
152.
153.
154.
155.
Me
.EnableHeadersVisualStyles =
True
156.
Me
.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
157.
Me
.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
158.
Me
.ColumnHeadersHeight = 40
159.
160.
End
Sub
161.
162.
#End Region
163.
164.
Public
Sub
SetNew(
ByVal
pDataSet
As
DataSet,
ByVal
pDataTable
As
DataTable,
ByVal
pBab
As
String
)
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
200.
201.
202.
203.
204.
205.
206.
207.
208.
209.
210.
211.
212.
213.
214.
215.
216.
217.
218.
219.
220.
221.
222.
223.
224.
225.
226.
227.
228.
229.
230.
231.
232.
233.
234.
235.
236.
237.
238.
239.
240.
241.
242.
243.
244.
245.
246.
247.
248.
249.
250.
251.
252.
253.
254.
255.
256.
257.
258.
259.
260.
261.
262.
263.
264.
265.
266.
267.
268.
269.
270.
271.
272.
273.
274.
275.
276.
277.
278.
End
Sub
279.
280.
281.
282.
Private
Function
GetS_field(
ByVal
S_Field
As
DataTable,
ByVal
pDataTableName
As
String
,
ByVal
pFieldName
As
String
)
As
DataRow
283.
Dim
foundRow()
As
DataRow
284.
Try
285.
foundRow = S_field.
Select
(
"C_file = '"
& pDataTableName &
"' And C_field = '"
& pFieldName &
"'"
,
"N_order Asc"
)
286.
Return
foundRow(0)
287.
Catch
ex
As
Exception
288.
Return
Nothing
289.
End
Try
290.
End
Function
291.
292.
293.
294.
Private
Sub
SetFormatGrid(
ByVal
i
As
UShort,
ByVal
S_fieldDataRow
As
DataRow,
ByVal
cFieldType
As
String
,
ByVal
cFieldName
As
String
)
295.
296.
Dim
colTextBox
As
New
DataGridViewTextBoxColumn
297.
Dim
colImage
As
New
DataGridViewImageColumn
298.
Dim
cHeaderText
As
String
=
String
.Empty
299.
300.
Select
Case
cFieldType
301.
Case
"System.String"
,
"System.Decimal"
,
"System.DateTime"
302.
303.
colTextBox =
New
DataGridViewTextBoxColumn()
304.
colTextBox.Name = cFieldName
305.
colTextBox.DataPropertyName = colTextBox.Name
306.
307.
Me
.Columns.Add(colTextBox)
308.
309.
colTextBox.SortMode = DataGridViewColumnSortMode.Programmatic
310.
311.
312.
If
S_fieldDataRow IsNot
Nothing
Then
313.
314.
If
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_Caption1"
)).Trim() <>
String
.Empty
Then
315.
cHeaderText =
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_Caption1"
)).Trim
316.
End
If
317.
318.
If
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_Caption2"
)).Trim() <>
String
.Empty
Then
319.
cHeaderText &= vbCrLf &
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_Caption2"
)).Trim
320.
End
If
321.
322.
colTextBox.HeaderText = cHeaderText
323.
324.
Select
Case
cFieldType
325.
Case
"System.String"
326.
Select
Case
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_align"
)).Trim()
327.
Case
"R"
328.
colTextBox.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
329.
Case
"C"
330.
colTextBox.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
331.
Case
Else
332.
colTextBox.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
333.
End
Select
334.
Case
"System.Decimal"
335.
Select
Case
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_align"
)).Trim
336.
Case
"L"
337.
colTextBox.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
338.
Case
"C"
339.
colTextBox.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
340.
Case
Else
341.
colTextBox.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
342.
End
Select
343.
344.
If
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_formatx"
)).Trim() =
String
.Empty
Then
345.
colTextBox.DefaultCellStyle.Format =
"N2"
346.
Else
347.
colTextBox.DefaultCellStyle.Format =
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_formatx"
)).Trim
348.
End
If
349.
350.
Case
"System.DateTime"
351.
Select
Case
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_align"
)).Trim
352.
Case
"L"
353.
colTextBox.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
354.
Case
"R"
355.
colTextBox.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
356.
Case
Else
357.
colTextBox.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
358.
End
Select
359.
360.
If
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_formatx"
)).Trim() =
String
.Empty
Then
361.
colTextBox.DefaultCellStyle.Format =
"dd MMM yyyy"
362.
Else
363.
colTextBox.DefaultCellStyle.Format =
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_formatx"
)).Trim()
364.
End
If
365.
End
Select
366.
367.
If
Not
String
.Format(
"{0:N0}"
, S_fieldDataRow.Item(
"N_gwidth"
)) =
"0"
Then
368.
colTextBox.Width =
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"N_gwidth"
))
369.
End
If
370.
371.
Else
372.
373.
Select
Case
cFieldType
374.
Case
"System.String"
375.
colTextBox.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
376.
Case
"System.Decimal"
377.
colTextBox.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
378.
colTextBox.DefaultCellStyle.Format =
"N2"
379.
Case
"System.DateTime"
380.
colTextBox.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
381.
colTextBox.DefaultCellStyle.Format =
"dd MMMM yyyy"
382.
End
Select
383.
384.
End
If
385.
386.
Case
"System.Byte[]"
387.
colImage =
New
DataGridViewImageColumn
388.
colImage.Name = cFieldName
389.
colImage.DataPropertyName = colImage.Name
390.
391.
Me
.Columns.Add(colImage)
392.
393.
If
S_fieldDataRow IsNot
Nothing
Then
394.
395.
If
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_Caption1"
)).Trim() <>
String
.Empty
Then
396.
cHeaderText =
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_Caption1"
)).Trim
397.
End
If
398.
399.
If
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_Caption2"
)).Trim() <>
String
.Empty
Then
400.
cHeaderText &= vbCrLf &
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_Caption2"
)).Trim
401.
End
If
402.
403.
colImage.HeaderText = cHeaderText
404.
405.
If
Not
String
.Format(
"{0:N0}"
, S_fieldDataRow.Item(
"N_gwidth"
)) =
"0"
Then
406.
colImage.Width =
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"N_gwidth"
))
407.
End
If
408.
Else
409.
410.
411.
412.
End
If
413.
End
Select
414.
End
Sub
415.
416.
417.
418.
Private
Sub
SetFormatGridAll(
ByVal
i
As
UShort,
ByVal
S_fieldDataRow
As
DataRow,
ByVal
cFieldType
As
String
,
ByVal
cFieldName
As
String
,
ByVal
pcol
As
DataGridViewColumn)
419.
420.
Dim
cHeaderText
As
String
=
String
.Empty
421.
422.
Select
Case
cFieldType
423.
424.
Case
"System.String"
,
"System.Decimal"
,
"System.DateTime"
425.
426.
If
S_fieldDataRow IsNot
Nothing
Then
427.
428.
If
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_Caption1"
)).Trim <>
String
.Empty
Then
429.
cHeaderText =
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_Caption1"
)).Trim
430.
End
If
431.
432.
If
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_Caption2"
)).Trim <>
String
.Empty
Then
433.
cHeaderText &= vbCrLf &
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_Caption2"
)).Trim
434.
End
If
435.
436.
pcol.HeaderText = cHeaderText
437.
438.
Select
Case
cFieldType
439.
Case
"System.String"
440.
Select
Case
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_align"
)).Trim
441.
Case
String
.Empty,
"L"
442.
pcol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
443.
Case
"R"
444.
pcol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
445.
Case
"C"
446.
pcol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
447.
End
Select
448.
Case
"System.Decimal"
449.
Select
Case
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_align"
)).Trim()
450.
Case
String
.Empty,
"R"
451.
pcol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
452.
Case
"L"
453.
pcol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
454.
Case
"C"
455.
pcol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
456.
End
Select
457.
458.
If
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_formatx"
)).Trim() =
String
.Empty
Then
459.
pcol.DefaultCellStyle.Format =
"N2"
460.
Else
461.
pcol.DefaultCellStyle.Format =
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_formatx"
)).Trim
462.
End
If
463.
464.
Case
"System.DateTime"
465.
Select
Case
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_align"
)).Trim()
466.
Case
String
.Empty,
"C"
467.
pcol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
468.
Case
"L"
469.
pcol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
470.
Case
"R"
471.
pcol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
472.
End
Select
473.
474.
If
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_formatx"
)).Trim() =
String
.Empty
Then
475.
pcol.DefaultCellStyle.Format =
"dd MMM yyyy"
476.
Else
477.
pcol.DefaultCellStyle.Format =
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_formatx"
)).Trim
478.
End
If
479.
End
Select
480.
481.
If
Not
String
.Format(
"{0:N0}"
, S_fieldDataRow.Item(
"N_gwidth"
)) =
"0"
Then
482.
pcol.Width =
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"N_gwidth"
))
483.
End
If
484.
485.
Else
486.
487.
Select
Case
cFieldType
488.
Case
"System.String"
489.
pcol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
490.
Case
"System.Decimal"
491.
pcol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
492.
pcol.DefaultCellStyle.Format =
"N2"
493.
Case
"System.DateTime"
494.
pcol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
495.
pcol.DefaultCellStyle.Format =
"dd MMM yyyy"
496.
End
Select
497.
End
If
498.
499.
Case
"System.Byte[]"
500.
501.
If
S_fieldDataRow IsNot
Nothing
Then
502.
503.
If
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_Caption1"
)).Trim <>
String
.Empty
Then
504.
cHeaderText =
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_Caption1"
)).Trim
505.
End
If
506.
507.
If
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_Caption2"
)).Trim <>
String
.Empty
Then
508.
cHeaderText &= vbCrLf &
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"C_Caption2"
)).Trim
509.
End
If
510.
511.
pcol.HeaderText = cHeaderText
512.
513.
If
Not
String
.Format(
"{0:N0}"
, S_fieldDataRow.Item(
"N_gwidth"
)) =
"0"
Then
514.
pcol.Width =
String
.Format(
"{0}"
, S_fieldDataRow.Item(
"N_gwidth"
))
515.
End
If
516.
Else
517.
518.
519.
520.
End
If
521.
End
Select
522.
End
Sub
523.
524.
525.
526.
527.
Private
Sub
usrCtrGridControl_EditingControlShowing(
ByVal
sender
As
Object
,
ByVal
e
As
System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)
Handles
Me
.EditingControlShowing
528.
If
Me
._DisplayManager = DataGridViewDisplayManager.Framework
AndAlso
Me
.
ReadOnly
=
False
Then
529.
Try
530.
_CellIsBeginEdit =
True
531.
_CellIsBeginEditRow =
Me
.CurrentCell.RowIndex
532.
_CellIsBeginEditCol =
Me
.CurrentCell.ColumnIndex
533.
534.
Catch
535.
536.
End
Try
537.
End
If
538.
End
Sub
539.
540.
541.
542.
Private
Sub
usrCtrGridControl_KeyPress(
ByVal
sender
As
Object
,
ByVal
e
As
System.Windows.Forms.KeyPressEventArgs)
Handles
Me
.KeyPress
543.
544.
545.
546.
547.
548.
549.
550.
551.
552.
553.
554.
555.
556.
557.
558.
559.
560.
561.
562.
563.
564.
565.
566.
567.
568.
569.
570.
571.
572.
573.
574.
575.
576.
577.
578.
579.
580.
581.
582.
583.
584.
585.
586.
587.
588.
589.
590.
591.
592.
593.
594.
595.
596.
597.
598.
599.
600.
601.
602.
603.
604.
605.
606.
607.
608.
609.
610.
611.
612.
613.
614.
615.
616.
617.
618.
619.
620.
621.
622.
623.
624.
End
Sub
625.
626.
627.
628.
Private
Sub
usrCtrGridControl_KeyDown(
ByVal
sender
As
Object
,
ByVal
e
As
System.Windows.Forms.KeyEventArgs)
Handles
Me
.KeyDown
629.
If
_DisplayManager = DataGridViewDisplayManager.Framework
AndAlso
Me
.
ReadOnly
=
False
Then
630.
If
_OnEnterModeMove = DataGridViewOnEnterModeMove.Right
Then
631.
Try
632.
If
Not
Me
.CurrentCell.IsInEditMode
Then
633.
634.
Dim
col
As
Short
=
Me
.CurrentCell.ColumnIndex
635.
636.
If
e.KeyCode = Keys.Tab
Then
637.
e.Handled =
True
638.
Exit
Sub
639.
End
If
640.
641.
If
e.KeyCode = Keys.Enter
AndAlso
col = _LastColumnVisible
AndAlso
Me
.CurrentRow.IsNewRow =
False
Then
642.
Me
.CurrentCell =
Me
.Rows(
Me
.CurrentRow.Index + 1).Cells(_FirstColumnVisible)
643.
e.Handled =
True
644.
Exit
Sub
645.
End
If
646.
647.
If
e.KeyCode = Keys.Enter
OrElse
e.KeyCode = Keys.Right
Then
648.
For
iSub
As
Short
= col
To
Me
.ColumnCount - 1
649.
col = (col + 1)
Mod
Me
.ColumnCount
650.
If
Me
.Columns(col).Visible =
True
AndAlso
Me
.Columns(col).
ReadOnly
=
False
AndAlso
_
651.
Me
.Columns(col).CellType.ToString <>
"System.Windows.Forms.DataGridViewImageCell"
Then
652.
Exit
For
653.
End
If
654.
Next
655.
If
Me
.Columns(col).Visible =
False
OrElse
Me
.Columns(col).
ReadOnly
=
True
OrElse
_
656.
Me
.Columns(col).CellType.ToString =
"System.Windows.Forms.DataGridViewImageCell"
Then
657.
658.
For
iSub
As
Short
= col
To
Me
.ColumnCount - 1
659.
col = (col + 1)
Mod
Me
.ColumnCount
660.
If
Me
.Columns(col).Visible =
True
AndAlso
Me
.Columns(col).
ReadOnly
=
False
AndAlso
_
661.
Me
.Columns(col).CellType.ToString <>
"System.Windows.Forms.DataGridViewImageCell"
Then
662.
Exit
For
663.
End
If
664.
Next
665.
End
If
666.
ElseIf
e.KeyCode = Keys.Left
Then
667.
For
iSub
As
Short
= col
To
0
Step
-1
668.
col = (col - 1)
Mod
Me
.ColumnCount
669.
If
col < 0
Then
670.
col =
Me
.ColumnCount - 1
671.
End
If
672.
If
Me
.Columns(col).Visible =
True
AndAlso
Me
.Columns(col).
ReadOnly
=
False
AndAlso
_
673.
Me
.Columns(col).CellType.ToString <>
"System.Windows.Forms.DataGridViewImageCell"
Then
674.
Exit
For
675.
End
If
676.
Next
677.
If
Me
.Columns(col).Visible =
False
OrElse
Me
.Columns(col).
ReadOnly
=
True
OrElse
_
678.
Me
.Columns(col).CellType.ToString =
"System.Windows.Forms.DataGridViewImageCell"
Then
679.
680.
For
iSub
As
Short
= col
To
Me
.ColumnCount - 1
681.
col = (col + 1)
Mod
Me
.ColumnCount
682.
If
Me
.Columns(col).Visible =
True
AndAlso
Me
.Columns(col).
ReadOnly
=
False
AndAlso
_
683.
Me
.Columns(col).CellType.ToString <>
"System.Windows.Forms.DataGridViewImageCell"
Then
684.
Exit
For
685.
End
If
686.
Next
687.
End
If
688.
End
If
689.
690.
If
e.KeyCode = Keys.Enter
OrElse
e.KeyCode = Keys.Left
OrElse
e.KeyCode = Keys.Right
Then
691.
Me
.CurrentCell =
Me
.CurrentRow.Cells(col)
692.
e.Handled =
True
693.
End
If
694.
End
If
695.
Catch
696.
697.
End
Try
698.
End
If
699.
End
If
700.
End
Sub
701.
702.
703.
704.
Private
Sub
usrCtrGridControl_CellMouseClickORDbClick(
ByVal
sender
As
Object
,
ByVal
e
As
System.Windows.Forms.DataGridViewCellMouseEventArgs)
Handles
Me
.CellMouseClick,
Me
.CellMouseDoubleClick
705.
If
_DisplayManager = DataGridViewDisplayManager.Framework
AndAlso
Me
.
ReadOnly
=
False
Then
706.
Try
707.
Dim
col
As
Short
=
Me
.CurrentCell.ColumnIndex
708.
709.
If
Me
.Columns(col).Visible =
False
OrElse
Me
.Columns(col).
ReadOnly
=
True
OrElse
_
710.
Me
.Columns(col).CellType.ToString =
"System.Windows.Forms.DataGridViewImageCell"
Then
711.
712.
For
iSub
As
Short
= col
To
Me
.ColumnCount - 1
713.
col = (col + 1)
Mod
Me
.ColumnCount
714.
If
Me
.Columns(col).Visible =
True
AndAlso
Me
.Columns(col).
ReadOnly
=
False
AndAlso
_
715.
Me
.Columns(col).CellType.ToString <>
"System.Windows.Forms.DataGridViewImageCell"
Then
716.
Exit
For
717.
End
If
718.
Next
719.
End
If
720.
Me
.CurrentCell =
Me
.CurrentRow.Cells(col)
721.
Catch
ex
As
Exception
722.
723.
End
Try
724.
End
If
725.
End
Sub
726.
727.
728.
729.
730.
731.
732.
733.
734.
735.
736.
737.
738.
739.
740.
741.
742.
743.
744.
745.
746.
747.
748.
749.
750.
751.
752.
753.
754.
755.
Private
Sub
DataGridView_SelectionChanged(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Me
.SelectionChanged
756.
757.
If
Me
._DisplayManager = DataGridViewDisplayManager.Framework
AndAlso
Me
.
ReadOnly
=
False
Then
758.
Try
759.
If
Me
.RowCount = 1
Then
760.
Me
.CurrentCell =
Me
.Rows(
Me
._FirstColumnVisible).Cells(
Me
._FirstColumnVisible)
761.
Exit
Sub
762.
End
If
763.
764.
Dim
subCol
As
Short
=
Me
.CurrentCell.ColumnIndex
765.
766.
If
_CellIsBeginEdit
AndAlso
_CellIsBeginEditCol <>
Me
._LastColumnVisible
Then
767.
768.
For
iSub
As
Short
= subCol
To
Me
.ColumnCount - 1
769.
subCol = (subCol + 1)
Mod
Me
.ColumnCount
770.
If
Me
.Columns(subCol).Visible =
True
AndAlso
Me
.Columns(subCol).
ReadOnly
=
False
AndAlso
_
771.
Me
.Columns(subCol).CellType.ToString <>
"System.Windows.Forms.DataGridViewImageCell"
Then
772.
Exit
For
773.
End
If
774.
Next
775.
776.
Me
.CurrentCell =
Me
.Rows(_CellIsBeginEditRow).Cells(subCol)
777.
778.
ElseIf
_CellIsBeginEdit
AndAlso
_CellIsBeginEditCol = _LastColumnVisible
Then
779.
Try
780.
Me
.CurrentCell =
Me
.Rows(_CellIsBeginEditRow + 1).Cells(
Me
._FirstColumnVisible)
781.
Catch
782.
783.
End
Try
784.
ElseIf
Not
_CellIsBeginEdit
AndAlso
Me
.CurrentRow.IsNewRow
Then
785.
If
Me
.CurrentRow.Cells(_FirstColumnVisible).Value
Is
Nothing
OrElse
_
786.
IsDBNull(
Me
.CurrentRow.Cells(_FirstColumnVisible).Value)
OrElse
_
787.
String
.IsNullOrEmpty(
Me
.CurrentRow.Cells(_FirstColumnVisible).Value)
Then
788.
Me
.CurrentCell =
Me
.CurrentRow.Cells(_FirstColumnVisible)
789.
End
If
790.
End
If
791.
792.
Catch
ex
As
Exception
793.
794.
End
Try
795.
796.
_CellIsBeginEdit =
False
797.
798.
End
If
799.
End
Sub
800.
801.
802.
803.
Private
Sub
DataGridView_ColumnHeaderMouseClick(
ByVal
sender
As
Object
,
ByVal
e
As
System.Windows.Forms.DataGridViewCellMouseEventArgs)
Handles
Me
.ColumnHeaderMouseClick
804.
If
Me
._DisplayManager = DataGridViewDisplayManager.Framework
Then
805.
806.
Me
.Cursor = Cursors.WaitCursor
807.
Dim
booSubAllowUserToAddRows
As
Boolean
=
Me
.AllowUserToAddRows
808.
809.
Try
810.
811.
812.
813.
If
Me
.Columns(e.ColumnIndex).CellType.ToString.Trim =
"System.Windows.Forms.DataGridViewImageCell"
Then
814.
Return
815.
End
If
816.
817.
If
Me
.DataSource IsNot
Nothing
AndAlso
Me
.DataSource.
GetType
.Name =
"BindingSource"
Then
818.
Dim
ctrSubBindingSource
As
BindingSource =
Me
.DataSource
819.
Dim
iSubRowIndex
As
Integer
820.
Dim
objSubSearchValue
As
Object
821.
822.
If
ctrSubBindingSource.Count <= 1
Then
823.
Exit
Sub
824.
End
If
825.
826.
If
String
.Format(
"{0}"
,
Me
._OnSortedFindKey).Trim <>
String
.Empty
Then
827.
828.
Dim
dbSubDRv
As
DataRowView = ctrSubBindingSource.Current
829.
objSubSearchValue =
String
.Format(
"{0}"
, dbSubDRv.Item(
Me
._OnSortedFindKey)).Trim
830.
Else
831.
objSubSearchValue =
String
.Format(
"{0}"
,
Me
.CurrentRow.Cells(e.ColumnIndex).Value).Trim
832.
End
If
833.
834.
Me
.AllowUserToAddRows =
False
835.
836.
Dim
cColumnName
As
String
=
Me
.Columns(e.ColumnIndex).Name
837.
Dim
cSortPatternArray()
As
String
838.
Dim
cSortPatternString
As
String
=
String
.Empty
839.
840.
cSortPatternArray =
Me
.SortPattern(cColumnName).ToString.Split(
","
)
841.
842.
If
cSortPatternArray.Length > 0
Then
843.
If
cSortPatternArray.Length = 1
AndAlso
String
.Format(
"{0}"
, cSortPatternArray(0)) =
String
.Empty
Then
844.
cSortPatternString = cColumnName
845.
If
Me
.SortOrder = Windows.Forms.SortOrder.Descending
Then
846.
Me
.Sort(
Me
.Columns(e.ColumnIndex), ListSortDirection.Ascending)
847.
Else
848.
Me
.Sort(
Me
.Columns(e.ColumnIndex), ListSortDirection.Descending)
849.
End
If
850.
Else
851.
For
i
As
Integer
= 0
To
cSortPatternArray.Length - 1
852.
cSortPatternArray(i).Trim()
853.
If
Me
.SortOrder = Windows.Forms.SortOrder.Descending
Then
854.
cSortPatternString &= cSortPatternArray(i) &
" Asc"
&
If
(i < cSortPatternArray.Length - 1,
","
,
String
.Empty)
855.
Else
856.
cSortPatternString &= cSortPatternArray(i) &
" Desc"
&
If
(i < cSortPatternArray.Length - 1,
","
,
String
.Empty)
857.
End
If
858.
Next
859.
bsGrid.Sort = cSortPatternString
860.
End
If
861.
End
If
862.
863.
If
String
.Format(
"{0}"
,
Me
._OnSortedFindKey).Trim <>
String
.Empty
Then
864.
iSubRowIndex = ctrSubBindingSource.Find(
Me
._OnSortedFindKey.Trim, objSubSearchValue)
865.
Else
866.
iSubRowIndex = ctrSubBindingSource.Find(
Me
.Columns(e.ColumnIndex).Name, objSubSearchValue)
867.
End
If
868.
869.
If
iSubRowIndex > -1
Then
870.
ctrSubBindingSource.Position = iSubRowIndex
871.
Me
.Columns(e.ColumnIndex).DisplayIndex = 0
872.
Me
.FirstDisplayedScrollingRowIndex = iSubRowIndex
873.
End
If
874.
875.
End
If
876.
Catch
ex
As
Exception
877.
MessageBox.Show(ex.Message,
"จัดเรียงข้อมูลในกริดผิดพลาด"
, MessageBoxButtons.OK, MessageBoxIcon.
Error
)
878.
Finally
879.
Me
.Cursor = Cursors.
Default
880.
Me
.AllowUserToAddRows = booSubAllowUserToAddRows
881.
End
Try
882.
End
If
883.
End
Sub
884.
885.
886.
887.
Private
Sub
Me_DataError(
ByVal
sender
As
Object
,
ByVal
e
As
System.Windows.Forms.DataGridViewDataErrorEventArgs)
Handles
Me
.DataError
888.
Dim
strSubMsg
As
String
=
String
.Empty
889.
890.
If
e.Exception IsNot
Nothing
Then
891.
strSubMsg = e.Exception.Message
892.
End
If
893.
894.
If
strSubMsg.Contains(
"does not allow nulls"
)
OrElse
strSubMsg.Contains(
"to be unique"
)
Then
895.
MessageBox.Show(
"รหัสที่ป้อนมีอยู่แล้วหรือไม่มีค่า (null) กรุณาป้อนให้ถูกต้อง"
& Constants.vbCrLf & Constants.vbCrLf & strSubMsg,
"Warning"
, MessageBoxButtons.OK, MessageBoxIcon.
Error
)
896.
ElseIf
strSubMsg.Contains(
"not in a correct format"
)
Then
897.
MessageBox.Show(
"ป้อนข้อมูลไม่ถูกต้อง เช่น จำนวนเงินแต่ป้อนเป็นตัวอักษรเป็นต้น กรุณาป้อนให้ถูกต้อง"
& Constants.vbCrLf & Constants.vbCrLf & strSubMsg,
"Warning"
, MessageBoxButtons.OK, MessageBoxIcon.
Error
)
898.
Else
899.
900.
End
If
901.
902.
Try
903.
e.Cancel =
False
904.
If
Me
.DataSource IsNot
Nothing
AndAlso
Me
.DataSource.
GetType
.Name =
"BindingSource"
Then
905.
Dim
ctrSubBindingSource
As
BindingSource =
Me
.DataSource
906.
ctrSubBindingSource.ResumeBinding()
907.
End
If
908.
Me
.CancelEdit()
909.
Catch
ex
As
Exception
910.
MsgBox(ex.Message)
911.
912.
End
Try
913.
End
Sub
914.
915.
916.
917.
Private
Sub
Me_CellFormatting(
ByVal
sender
As
Object
,
ByVal
e
As
System.Windows.Forms.DataGridViewCellFormattingEventArgs)
Handles
Me
.CellFormatting
918.
Try
919.
If
Me
.Columns(e.ColumnIndex).ValueType.Name.ToString =
"Decimal"
Then
920.
If
Me
.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0
Then
921.
Me
.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.ForeColor = Color.White
922.
End
If
923.
ElseIf
Me
.Columns(e.ColumnIndex).ValueType.Name.ToString =
"DateTime"
Then
924.
Try
925.
If
String
.Format(
"{0:dd/MM/yyyy}"
,
Me
.Rows(e.RowIndex).Cells(e.ColumnIndex).Value).Trim =
"30/12/2442"
Then
926.
Me
.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.ForeColor = Color.White
927.
End
If
928.
Catch
ex
As
Exception
929.
930.
End
Try
931.
End
If
932.
933.
Catch
ex
As
Exception
934.
935.
End
Try
936.
End
Sub
937.
938.
939.
940.
Private
Sub
Me_RowPostPaint(
ByVal
sender
As
Object
,
ByVal
e
As
System.Windows.Forms.DataGridViewRowPostPaintEventArgs)
Handles
Me
.RowPostPaint
941.
942.
943.
944.
945.
946.
947.
948.
949.
950.
951.
952.
953.
954.
955.
956.
957.
End
Sub
958.
959.
End
Class