01.
Imports
System.Collections.Generic
02.
Imports
System.Linq
03.
Imports
System.Web
04.
Imports
System.Web.UI
05.
Imports
System.Web.UI.WebControls
06.
07.
Imports
System.Data
08.
09.
Public
Partial
Class
MyGridView
10.
Inherits
System.Web.UI.Page
11.
Private
dataSource
As
DataTable
12.
13.
Protected
Sub
Page_Init(sender
As
Object
, e
As
EventArgs)
14.
dataSource =
New
DataTable()
15.
End
Sub
16.
17.
Protected
Sub
Page_Load(sender
As
Object
, e
As
EventArgs)
18.
dataSource =
If
(ViewState(
"dataSource"
) IsNot
Nothing
,
DirectCast
(ViewState(
"dataSource"
), DataTable), GetDataSource())
19.
GridView1.DataSource = dataSource
20.
21.
Dim
Index
As
New
BoundField()
22.
Index.HeaderText =
"#"
23.
Index.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
24.
Index.ItemStyle.HorizontalAlign = HorizontalAlign.Right
25.
Index.DataField =
"ID"
26.
27.
Dim
MonthName
As
New
BoundField()
28.
MonthName.HeaderText =
"MonthName"
29.
MonthName.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
30.
MonthName.ItemStyle.HorizontalAlign = HorizontalAlign.Left
31.
MonthName.DataField =
"Month"
32.
33.
Dim
DeleteButtom
As
New
CommandField()
34.
DeleteButtom.HeaderText =
"Delete"
35.
DeleteButtom.ButtonType = ButtonType.Image
36.
DeleteButtom.ShowEditButton =
False
37.
DeleteButtom.ShowDeleteButton =
True
38.
DeleteButtom.DeleteImageUrl =
"~/images/delete-16x16.png"
39.
DeleteButtom.ItemStyle.HorizontalAlign = HorizontalAlign.Center
40.
DeleteButtom.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
41.
42.
GridView1.AutoGenerateColumns =
False
43.
GridView1.DataKeyNames =
New
String
() {
"ID"
}
44.
45.
If
Not
IsPostBack
Then
46.
ViewState(
"dataSource"
) = dataSource
47.
48.
GridView1.Columns.Add(Index)
49.
GridView1.Columns.Add(MonthName)
50.
GridView1.Columns.Add(DeleteButtom)
51.
GridView1.DataBind()
52.
End
If
53.
End
Sub
54.
55.
Protected
Sub
GridView1_RowDataBound(sender
As
Object
, e
As
GridViewRowEventArgs)
Handles
GridView1_RowDataBound
56.
If
e.Row.RowType = DataControlRowType.DataRow
Then
57.
Dim
DeleteButton
As
ImageButton =
DirectCast
(e.Row.Cells(2).Controls(0), ImageButton)
58.
DeleteButton.OnClientClick =
"javascript:return confirm('คุณต้องการลบข้อมูลนี้ใช่หรือไม่')"
59.
End
If
60.
End
Sub
61.
62.
Protected
Sub
GridView1_RowDeleting(sender
As
Object
, e
As
GridViewDeleteEventArgs)
Handles
GridView1.RowDeleting
63.
dataSource.Rows(e.RowIndex).Delete()
64.
dataSource.AcceptChanges()
65.
66.
GridView1.DataSource = dataSource
67.
GridView1.DataBind()
68.
69.
ViewState(
"dataSource"
) = dataSource
70.
End
Sub
71.
72.
Private
Function
GetDataSource()
As
DataTable
73.
Dim
Dt
As
New
DataTable()
74.
Dt.Columns.Add(
New
DataColumn(
"ID"
,
GetType
(
Integer
)))
75.
Dt.Columns.Add(
New
DataColumn(
"Month"
,
GetType
(
String
)))
76.
77.
For
i
As
Integer
= 1
To
12
78.
Dim
Dr
As
DataRow = Dt.NewRow()
79.
Dr(
"ID"
) = i.ToString()
80.
Dr(
"Month"
) = DateTime.Parse(
String
.Format(
"1/{0}/2010"
, i.ToString())).ToString(
"MMMM"
)
81.
Dt.Rows.Add(Dr)
82.
Next
83.
84.
Return
Dt
85.
End
Function
86.
End
Class