如何将编辑/删除/更新按钮和事件添加到gridview

时间:2012-09-19 15:26:32

标签: asp.net datagridview ado.net

我试图添加按钮或链接到gridview所以我可以编辑/删除/更新记录,但我无法添加它,我尝试了很多东西但我得到错误可以有人帮助我。 !? 我可以在gridview中显示记录/日期,但我无法看到如何添加按钮事件。

我的front_code是:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Data_Adapter_Grid.aspx.vb" Inherits="Data_Adapter_Grid" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
        CellPadding="4" DataKeyNames="test_id" ForeColor="Black" GridLines="Horizontal">
    <Columns>
        <asp:BoundField DataField="test_id" HeaderText="test_id" InsertVisible="False"
            ReadOnly="True" SortExpression="test_id" />
        <asp:BoundField DataField="test_cat" HeaderText="test_cat"
            SortExpression="test_cat" />
        <asp:BoundField DataField="test_info" HeaderText="test_info"
            SortExpression="test_info" />
        <asp:BoundField DataField="test_number" HeaderText="test_number"
            SortExpression="test_number" />
    </Columns>
    <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
    <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
    <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F7F7F7" />
    <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
    <SortedDescendingCellStyle BackColor="#E5E5E5" />
    <SortedDescendingHeaderStyle BackColor="#242121" />
    </asp:GridView>
</div>
</form>
</body>
</html>

我的后面代码是:

Imports System.Data.OleDb
Imports System.Data

Partial Class Data_Adapter_Grid
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        Call GetRecords()
    End If
End Sub
' "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\Peter\Documents\Visual Studio 2010\Projects\StockIT\StockIT\bin\Debug\StockManagement.accdb';Persist Security Info=True;Jet OLEDB:Database Password="
'Get all Records

Sub GetRecords()
    Dim strSQL As String = ""
    strSQL = "" & _
    "SELECT * FROM [TableTest] " & _
    "ORDER BY [test_id] ASC"

    Dim dt As New DataTable()
    Using conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("MyConnStr").ConnectionString)
        Try
            conn.Open()
            Dim cmd As New OleDbCommand(strSQL, conn)
            Dim dbAdapter As New OleDbDataAdapter(cmd)
            'Dim cb As New OleDbCommandBuilder(dbAdapter)
            dbAdapter.Fill(dt)  '(dt, "GridLoad")
            dbAdapter.Dispose()
            If dt.Rows.Count > 0 Then
                GridView1.DataSource = dt
                'GridView1.DataMember = "GridLoad"
                GridView1.DataBind()
            End If
        Catch exp As OleDbException
            If True Then
                MsgBox("Error trying to get records, maybe there is no records. " & vbCrLf & "Error: " & exp.Message & "Database Error", MsgBoxStyle.OkOnly, MsgBoxStyle.Critical)
            End If
        Catch exp As Exception
            If True Then
                MsgBox("Error the Database can be unavailable atm. " & vbCrLf & "Error: " & exp.Message & "Database Error", MsgBoxStyle.OkOnly, MsgBoxStyle.Information)
            End If
        End Try
    End Using
End Sub

Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs)

End Sub

Protected Sub GridView1_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs)

End Sub

Protected Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)

End Sub

Protected Sub GridView1_RowDelete(sender As Object, e As GridViewDeletedEventArgs)

End Sub


End Class

3 个答案:

答案 0 :(得分:0)

尝试在gridview上使用自定义按钮。波纹管链接为您提供更多解释样本: custom button on gridview

答案 1 :(得分:0)

尝试<asp:templatefield> // add button or link inside </asp:templatefield>调用按钮 onrowcommand 事件,阅读它以明确http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

答案 2 :(得分:0)

在gridview属性中设置这两个

AutoGenerateDeleteButton="True" 
AutoGenerateEditButton="True"

<asp:TemplateField>
   <FooterTemplate> // may be you want ItemTemplate 
     <asp:Button ID="btnUpdate" runat="server" CommandName="Update" Text="Update" ValidationGroup="validate" />
   </FooterTemplate>
</asp:TemplateField>

希望这有帮助!