Jag försöker att skapa en grid i codebehind genom att lägga till All denna hantering som du är ute efter kan skötas via automatiserad kod och nästan uteslutande skrivas i aspx-koden. Därför bör du nog baka med den koden också. Här kommer ASPX-koden, den var så trivial att jag inte tog med den.Datagrid och tilllagd EditCommandColumn
kolumner.
Jag lägger in två BoundColumn sedan
lägger jag till en EditCommandColumn.
När jag trycker Edit så kommer man in i editeringsläge med
Update och Cancel.
Trycker man sedan Update eller Cancel kommer man åter in i eventen för
Edit, vad gör jag fel.
<----------------------------------------- Code starts here -------------------------------- >
<code>
Protected WithEvents dgGrid As System.Web.UI.WebControls.DataGrid
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Property DynamicColumnAdded() As Boolean
Get
If ViewState("ColumnAdded") Is Nothing Then
Return False
Else
Return True
End If
End Get
Set(ByVal Value As Boolean)
ViewState("ColumnAdded") = Value
End Set
End Property
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Me.DynamicColumnAdded Then
AddColumns()
End If
End Sub
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState)
If DynamicColumnAdded Then
AddColumns()
End If
End Sub
Private Sub AddColumns()
Dim userIDColumn As New BoundColumn()
With userIDColumn
.DataField = "User"
.HeaderText = "User"
End With
dgGrid.Columns.Add(userIDColumn)
Dim passwordColumn As New BoundColumn()
With passwordColumn
.DataField = "Password"
.HeaderText = "Password"
End With
dgGrid.Columns.Add(passwordColumn)
Dim editColumn As New EditCommandColumn()
With editColumn
.EditText = "Edit"
.CancelText = "Cancel"
.UpdateText = "Update"
.HeaderText = "Edit"
End With
dgGrid.Columns.Add(editColumn)
dgGrid.DataSource = UserSet()
dgGrid.DataBind()
DynamicColumnAdded = True
End Sub
Private Function UserSet() As DataSet
Dim sql As String = "SELECT User,Password FROM tblUser"
Dim resultSet As DataSet = New DataSet()
Dim adapter As OleDbDataAdapter = New OleDbDataAdapter(sql, ConnectionString)
adapter.Fill(resultSet)
Return resultSet
End Function
Private Sub dgGrid_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgGrid.EditCommand
dgGrid.EditItemIndex = e.Item.ItemIndex
dgGrid.DataBind()
End Sub
Private Sub dgGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgGrid.DeleteCommand
Response.Write("Delete")
End Sub
Private Sub dgGrid_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgGrid.UpdateCommand
Response.Write("Update")
End Sub
Private Sub dgGrid_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgGrid.CancelCommand
dgGrid.EditItemIndex = -1
dgGrid.DataBind()
End Sub
</code>
<------------------------------------------ Code ends here --------------------------------------- >Sv: Datagrid och tilllagd EditCommandColumn
//Mikael.NETSv: Datagrid och tilllagd EditCommandColumn
Jag inser att det fungerar att göra det med designern, men jag
undrar varför det inte fungerar som jag har gjort.
<----------------------------------------- Code starts here -------------------------------- >
<code>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="GridTest.aspx.vb" Inherits="OF.GridTest" enableViewState="True"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>GridTest</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="dgGrid" style="Z-INDEX: 101; LEFT: 62px; POSITION: absolute; TOP: 73px" runat="server" Width="341px" Height="177px" AutoGenerateColumns="False">
<Columns></Columns>
</asp:DataGrid>
</form>
</body>
</HTML>
</code>
<------------------------------------------ Code ends here --------------------------------------- >