如何在GridView加入按钮来进行更新的操作!
说明:我用的数据源是Access数据源控件,而后我又在Gridview控件中加入ButtonField字段加入按钮,为了更新Gridview控件中的时间字段,而这个时间是当前的日期时间,没有涉及到编辑。在应用中,更新别的字段好用,而更新时间不起做用,也不提示错误,不知为什么:
代码如下:
后台的:
Protected Sub GridView2_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView2.RowCommand
If e.CommandName = "Update " Then
Dim cn As New OleDbConnection(yjClass.DbPath1)
Dim str As String = "Update info set fbTime=@d,infobt=@bt where id= " & e.CommandArgument.ToString
Dim cmd As New OleDbCommand(str, cn)
cmd.Parameters.Add( "@d ", OleDbType.Date)
cmd.Parameters( "@d ").Value = Date.Now
Try
cn.Open()
cmd.ExecuteNonQuery()
Finally
cn.Close()
End Try
End If
End Sub
e.CommandArgument.ToString 是按钮绑定的Id字段。
前台代码:
<asp:GridView ID= "GridView2 " runat= "server " AutoGenerateColumns= "False "
DataKeyNames= "Id " DataSourceID= "AccessDataSource1 " Width= "580px " BorderStyle= "None " BorderWidth= "0px " CellPadding= "4 " CellSpacing= "2 " >
<Columns>
<asp:TemplateField> </asp:TemplateField>
<asp:BoundField DataField= "Id " HeaderText= "Id " InsertVisible= "False " ReadOnly= "True "
SortExpression= "Id " />
<asp:TemplateField HeaderText= "信息标题 ">