如何添加编辑/删除/更新按钮,事件的GridView

问题描述:

我试着按钮或链接添加到一个GridView,这样我可以编辑/删除/更新记录,但我不能添加它,我已经tryed很多事情,但我得到错误可以有人帮助我。 !? 我可以显示在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> 

我behind_code是:

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 

尝试<asp:templatefield> // add button or link inside </asp:templatefield>呼叫按钮onrowcommand事件,读它,使自己清楚http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

尝试使用在GridView的自定义按钮。波纹管链接给你更多的解释样本: custom button on gridview

设置这两个在你的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> 

希望这有助于!

+0

很酷,但我怎么排序按钮列? – Krismorte