VB对GridView的一些操作

View Code
  1 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
2
3 If Not IsPostBack Then
4
5 binddata()
6
7 End If
8
9
10
11 End Sub
12
13
14
15 Public Sub binddata()
16
17 '
18
19 End Sub
20
21
22
23 '编辑中
24
25 Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
26
27 GridView1.EditIndex = e.NewEditIndex
28
29 '当前编辑行背景色高亮
30
31 GridView1.EditRowStyle.BackColor = Color.FromName("#F7CE90")
32
33 binddata()
34
35
36
37 End Sub
38
39
40
41 '分页
42
43 Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
44
45 GridView1.PageIndex = e.NewPageIndex
46
47 binddata() '重新绑定GridView数据的函数
48
49 End Sub
50
51
52
53 '更新
54
55 Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
56
57 Dim clsB As New sqlDLTP.business
58
59 Dim sqlstr As String
60
61 Dim ds As New DataSet
62
63 sqlstr = "select hbdwno from etsshbd where hbdcnm='" & CType(GridView1.Rows(e.RowIndex).FindControl("TextBox1"), TextBox).Text.ToString().Trim() & "'"
64
65 ds = clsB.queryitems(sqlstr)
66
67 Dim wno As String = ds.Tables(0).Rows(0)(0).ToString().Trim()
68
69
70
71 sqlstr = "update etsdl set okscore1='" & _
72
73 CType(GridView1.Rows(e.RowIndex).FindControl("TextBox6"), TextBox).Text.ToString().Trim() & "',okscore2='" & _
74
75 CType(GridView1.Rows(e.RowIndex).FindControl("TextBox7"), TextBox).Text.ToString().Trim() & "',okscore='" & _
76
77 CType(GridView1.Rows(e.RowIndex).FindControl("TextBox8"), TextBox).Text.ToString().Trim() & "',okreport='" & _
78
79 CType(GridView1.Rows(e.RowIndex).FindControl("TextBox9"), TextBox).Text.ToString().Trim() & "',okgrad='" & _
80
81 CType(GridView1.Rows(e.RowIndex).FindControl("TextBox10"), TextBox).Text.ToString().Trim() & "',memo='" & _
82
83 CType(GridView1.Rows(e.RowIndex).FindControl("TextBox12"), TextBox).Text.ToString().Trim() & "' where trano='" & _
84
85 GridView1.DataKeys(e.RowIndex).Value.ToString() & "' and wno='" & wno & "'"
86
87 clsB.ExeSqlCmd(sqlstr)
88
89 GridView1.EditIndex = -1
90
91 binddata()
92
93 End Sub
94
95
96
97 '取消
98
99 Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
100
101 GridView1.EditIndex = -1
102
103 binddata()
104
105 End Sub
106
107
108
109 '删除
110
111 Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
112
113 Dim clsB As New sqlDLTP.business
114
115 Dim sqlstr As String
116
117 Dim ds As New DataSet
118
119 sqlstr = "select hbdwno from etsshbd where hbdcnm='" & CType(GridView1.Rows(e.RowIndex).FindControl("Label1"), Label).Text.ToString().Trim() & "'"
120
121 ds = clsB.queryitems(sqlstr)
122
123 Dim wno As String = ds.Tables(0).Rows(0)(0).ToString().Trim()
124
125
126
127 sqlstr = "delete etsdl where trano=" & GridView1.DataKeys(e.RowIndex).Value.ToString().Trim() & "and wno='" & wno & "'"
128
129 clsB.ExeSqlCmd(sqlstr)
130
131 binddata()
132
133 End Sub
134
135
136
137 '绑定行,特效及链接列属性分配等
138
139 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
140
141 If e.Row.RowType = DataControlRowType.DataRow Then
142
143
144
145 '鼠标经过时,行背景色变
146
147 e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'")
148
149 '鼠标移出时,行背景色变
150
151 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'")
152
153 ' HyperLink列给链接值
154
155 CType(e.Row.Cells(1).FindControl("HyperLink1"), HyperLink).NavigateUrl = "javascript:void window.open('trashow.aspx?tno=" + CType(e.Row.Cells(1).FindControl("HyperLink1"), HyperLink).Text + "','', 'left='+(window.top.screen.width-454)/2+',top='+(window.top.screen.height-454)/2+',width=625,height=500,scrollbars=yes,resizeable=yes');"
156
157 '当有编辑列时,避免出错,要加的RowState判断
158
159 If e.Row.RowState = DataControlRowState.Normal Or e.Row.RowState = DataControlRowState.Alternate Then
160
161
162
163 If CType(e.Row.Cells(12).FindControl("Label11"), Label).Text = "1" Then
164
165 CType(e.Row.Cells(12).FindControl("Label11"), Label).Text = "在职"
166
167 End If
168
169 End If
170
171 End If
172
173 End Sub



前台:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width=100% BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" AllowPaging="True" AllowSorting="True">

你可能感兴趣的:(GridView)