Compare Page Revisions
« Older Revision - Back to Page History - Current Revision
Table of Contents [Hide/Show]
The ASP:GridView Tag DataBinding Code Grid Columns Adding Editing Functions The Edit Button The Cancel Button The Update Button The Delete Button Client-Side (User Confirmation) Server Side (Data Access)
<asp:GridView runat=server" ID="uxMyGridView" AutoGenerateColumns="false" CellPadding="3" CellSpacing="0" DataKeyNames="MyPrimaryKeyField(s)"> </asp:GridView>
myDataTable
DataTable
uxMyGridView.DataSource = myDataTable; uxMyGridView.DataBind();
<Columns>
<asp:GridView>
asp:BoundField
ReadOnly
true
asp:TemplateField
Eval
asp:CommandField
<asp:GridView runat=server" ID="uxMyGridView" AutoGenerateColumns="false" CellPadding="3" CellSpacing="0" DataKeyNames="MyPrimaryKeyField(s)" > <Columns> <asp:BoundField DataField="MyDataField" HeaderText="MyHeader" /> <asp:TemplateField HeaderText="Description"> <ItemTemplate> <asp:Label runat=server Text='<%# Eval("Description") %>' /> </ItemTemplate> <EditItemTemplate> <asp:TextBox runat="server" ID="uxDescriptionTextBox" Text='<%# Eval("Description") %>' /> </EditItemTemplate> </asp:TemplateField> <asp:CommandField HeaderText="Action" ButtonType="Image" DeleteText="Delete" ShowDeleteButton="true" DeleteImageUrl="~/images/Delete.png" EditText="Edit" ShowEditButton="true" EditImageUrl="~/images/Pencil.png" CancelText="Cancel" ShowCancelButton="true" CancelImageUrl="~/images/StopSign.png" UpdateText="Save" UpdateImageUrl="~/images/Save.png" ShowSelectButton="false" /> </Columns> </asp:GridView>
OnRowEditing
asp:GridView
Protected Sub uxMyGridView_RowEditing(ByVal sender As Object, ByVal e As _ GridViewEditEventArgs) Handles uxMyGridView.RowEditing Dim grid As GridView = CType(sender, GridView) grid.EditIndex = e.NewEditIndex BindMyGrid() End Sub
OnRowCancelingEdit
Protected Sub uxMyGridView_RowEditingCancelled(ByVal sender As Object, ByVal e As _ GridViewCancelEditEventArgs) Handles uxMyGridView.RowCancelingEdit Dim grid As GridView = CType(sender, GridView) grid.EditIndex = -1 BindMyGrid() End Sub
OnRowUpdating
Keys
OldValues
NewValues
GridViewUpdateEventArgs
DataSourceID
Protected Sub uxMyGridView_RowUpdate(ByVal sender As Object, ByVal e As _ GridViewUpdateEventArgs) Handles uxMyGridView.RowUpdating Dim gv As GridView = CType(sender, GridView) Dim gvRow As GridViewRow = gridView.Rows(e.RowIndex) Dim rowKey as DataKey = gv.DataKeys(e.RowIndex) Dim id as Integer = Convert.ToInt32(rowKey("ID")) dim description as String = CType(gvRow.FindControl("uxDescriptionTextBox"), TextBox).Text ' Submit data to database for updating gv.EditIndex = -1 BindMyGrid() End Sub
deleteButton
Button
ImageButton
LinkButton
<asp:CommandField>
.Cells()
.Controls()
Protected Sub uxMyGridView_RowDataBound(ByVal sender As Object, ByVal e As _ GridViewRowEventArgs) Handles uxMyGridView.RowDataBound Try If e.Row.RowType = DataControlRowType.DataRow Then Dim dataRow As DataRow = CType(e.Row.DataItem, DataRowView).Row Dim fileName As String = dataRow("FileName").ToString() Dim deleteButton As ImageButton = e.Row.Cells(3).Controls(2) deleteButton.OnClientClick = "confirm('Delete [" & fileName & "]?');" End If Catch ex As Exception Throw ex End Try End Sub
OnRowDeleting
Protected Sub uxMyGridView_RowDeleting(ByVal sender As Object, ByVal e As _ GridViewDeleteEventArgs) Handles uxMyGridView.RowDeleting Dim gv As GridView = CType(sender, GridView) Dim gvRow As GridViewRow = gv.Rows(e.RowIndex) Dim rowKey As DataKey = gv.DataKeys(e.RowIndex) Dim id As Integer = Convert.ToInt32(rowKey("ID")) Globals.Database.DeleteItem(id) gv.EditIndex = -1 BindMyGrid() End Sub
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.