【轉】VB.NET gridview使用

【轉】VB.NET gridview使用

【Vegas原创】VB.NET版的GridView经典使用(编辑,删除,分页,链接列)Protected Sub Page_Load(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
binddata()
End If

End Sub

Public Sub binddata()
'
End Sub

'编辑中
Protected Sub GridView1_RowEditing(ByVal sender As Object,ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
'当前编辑行背景色高亮
GridView1.EditRowStyle.BackColor = Color.FromName("#F7CE90")
binddata()

End Sub

'分页
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object,ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
GridView1.PageIndex = e.NewPageIndex
binddata() '重新绑定GridView数据的函数
End Sub

'更新
Protected Sub GridView1_RowUpdating(ByVal sender As Object,ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim clsB As New sqlDLTP.business
Dim sqlstr As String
Dim ds As New DataSet
sqlstr = "select hbdwno from etsshbd where hbdcnm='" & CType(GridView1.Rows(e.RowIndex).FindControl("TextBox1"),TextBox).Text.ToString().Trim() & "'"
ds = clsB.queryitems(sqlstr)
Dim wno As String = ds.Tables(0).Rows(0)(0).ToString().Trim()

sqlstr = "update etsdl set okscore1='" & _
CType(GridView1.Rows(e.RowIndex).FindControl("TextBox6"),TextBox).Text.ToString().Trim() & "',okscore2='" & _
CType(GridView1.Rows(e.RowIndex).FindControl("TextBox7"),okscore='" & _
CType(GridView1.Rows(e.RowIndex).FindControl("TextBox8"),okreport='" & _
CType(GridView1.Rows(e.RowIndex).FindControl("TextBox9"),okgrad='" & _
CType(GridView1.Rows(e.RowIndex).FindControl("TextBox10"),memo='" & _
CType(GridView1.Rows(e.RowIndex).FindControl("TextBox12"),TextBox).Text.ToString().Trim() & "' where trano='" & _
GridView1.DataKeys(e.RowIndex).Value.ToString() & "' and wno='" & wno & "'"
clsB.ExesqlCmd(sqlstr)
GridView1.EditIndex = -1
binddata()
End Sub

'取消
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object,ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
GridView1.EditIndex = -1
binddata()
End Sub

'删除
Protected Sub GridView1_RowDeleting(ByVal sender As Object,ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Dim clsB As New sqlDLTP.business
Dim sqlstr As String
Dim ds As New DataSet
sqlstr = "select hbdwno from etsshbd where hbdcnm='" & CType(GridView1.Rows(e.RowIndex).FindControl("Label1"),Label).Text.ToString().Trim() & "'"
ds = clsB.queryitems(sqlstr)
Dim wno As String = ds.Tables(0).Rows(0)(0).ToString().Trim()

sqlstr = "delete etsdl where trano=" & GridView1.DataKeys(e.RowIndex).Value.ToString().Trim() & "and wno='" & wno & "'"
clsB.ExesqlCmd(sqlstr)
binddata()
End Sub

'绑定行,特效及链接属性分配等
Protected Sub GridView1_RowDataBound(ByVal sender As Object,ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then

'鼠标经过时,行背景色变
e.Row.Attributes.Add("onmouSEOver","this.style.backgroundColor='#E6F5FA'")
'鼠标移出时,行背景色变
e.Row.Attributes.Add("onmouSEOut","this.style.backgroundColor='#FFFFFF'")
' HyperLink列给链接
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');"
'当有编辑列时,避免出错,要加的RowState判断
If e.Row.RowState = DataControlRowState.Normal Or e.Row.RowState = DataControlRowState.Alternate Then

If CType(e.Row.Cells(12).FindControl("Label11"),Label).Text = "1" Then
CType(e.Row.Cells(12).FindControl("Label11"),Label).Text = "在职"
End If
End If
End If
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">
<!-- --> </asp:GridView>

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强制返回为文本 --------------------------...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办法, Format 或者FormatDateTime 竟然结果和...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace My ‘全局错误处理,新的解决方案直接...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用的爽呀,这篇文章写与2011年,看来我以前没...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选中的单元格进行处理 Dim m As Range, t...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integ...