使用 Sql数据源编辑不带 Gridview VB.Net



我开发了以下代码来编辑 GridView(遵循用 C# 编写的教程),它进入编辑模式,但我的编辑没有生效,这是我的代码:

ASPX.vb代码:

Imports System.Data
Imports System.Data.SqlClient
Imports System.Globalization
Partial Class MemberPages_editOutage
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not IsPostBack Then
            BindGrid()
        End If
    End Sub
    Private Sub BindGrid()
        Dim dt As New DataTable()
        Dim connection As New SqlConnection("server='SQLEXPRESS'; trusted_connection='true'; Database='OutagesMgt_db'")
        Try
            connection.Open()
            Dim sqlStatement As String = "SELECT OutageDetailId, LocationName, Description, DetailDescription, CreateDate, StatusId FROM OutageDetail WHERE StatusId='1' ORDER BY CreateDate DESC"
            Dim cmd As New SqlCommand(sqlStatement, connection)
            Dim sqlDa As New SqlDataAdapter(cmd)
            sqlDa.Fill(dt)
            If dt.Rows.Count > 0 Then
                MyDataGrid.DataSource = dt
                MyDataGrid.DataBind()
            End If
        Catch ex As System.Data.SqlClient.SqlException
            Dim msg As String = "Fetch Error:"
            msg += ex.Message
            Throw New Exception(msg)
        Finally
            connection.Close()
        End Try
    End Sub
    'edit command
    Protected Sub MyDataGrid_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles MyDataGrid.RowEditing
        'turn to edit mode
        MyDataGrid.EditIndex = e.NewEditIndex
        'Rebind the GridView to show the data in edit mode
        BindGrid()
    End Sub
    'cancel command
    Protected Sub MyDataGrid_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles MyDataGrid.RowCancelingEdit
        ' switch back to edit default mode
        MyDataGrid.EditIndex = -1
        'Rebind the GridView to show the data in edit mode
        BindGrid()
    End Sub
    'Update Function
    Private Sub UpdateRecord(ByVal SOutageDetailId As String, ByVal SDescription As String, ByVal SDetailDescription As String, ByVal SCreateDate As String, ByVal SstatusId As String)
        Dim connection As New SqlConnection("server='SQLEXPRESS'; trusted_connection='true'; Database='OutagesMgt_db'")
        Dim sqlStatement As String = String.Empty
        sqlStatement = "UPDATE OutageDetail SET @OutageDetailId = @OutageDetailId, LocationName = @LocationName, " & _
                        "Description = @Description, DetailDescription= @DetailDescription, " & _
                        "CreateDate = @CreateDate, StatusId = @StatusId WHERE OutageDetailId = @OutageDetailId"
        connection.Open()
        Dim cmd As New SqlCommand(sqlStatement, connection)
        cmd.Parameters.Add(New SqlParameter("@OutageDetailId", SOutageDetailId))
        cmd.Parameters.Add(New SqlParameter("@LocationName", SDescription))
        cmd.Parameters.Add(New SqlParameter("@Description", SDescription))
        cmd.Parameters.Add(New SqlParameter("@DetailDescription", SDetailDescription))
        cmd.Parameters.Add(New SqlParameter("@CreateDate", SCreateDate))
        cmd.Parameters.Add(New SqlParameter("@StatusId", SstatusId))
        cmd.CommandType = CommandType.Text
        cmd.ExecuteNonQuery()

        ' MyDataGrid.EditIndex = -1
        connection.Close()
        BindGrid()
    End Sub
    'update command
    Protected Sub MyDataGrid_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles MyDataGrid.RowUpdating
        'Accessing Edited values from the GridView
        Dim SOutageDetailId As String = MyDataGrid.Rows(e.RowIndex).Cells(0).Text
        Dim SDescription As String = MyDataGrid.Rows(e.RowIndex).Cells(1).Text
        Dim SDetailDescription As String = MyDataGrid.Rows(e.RowIndex).Cells(2).Text
        Dim SCreateDate As String = MyDataGrid.Rows(e.RowIndex).Cells(3).Text
        Dim SstatusId As String = MyDataGrid.Rows(e.RowIndex).Cells(4).Text
        'Call the function to update the GridView
        UpdateRecord(SOutageDetailId, SDescription, SDetailDescription, SCreateDate, SstatusId)
        MyDataGrid.EditIndex = -1
        'Rebind Gridview to reflect changes made
        BindGrid()
    End Sub
End Class

ASPX 代码:

<asp:GridView id="MyDataGrid" runat="server"
                    Width="750px"
                    CssClass="gridViewEdit"
                    BackColor="White"
                    BorderColor="Black"
                    CellPadding="3"
                    Font-Name="Verdana"
                    Font-Size="8pt"
                    HeaderStyle-BackColor="#FFFFFF"
                    OnEditCommand="MyDataGrid_RowEditing"
                    OnCancelCommand="MyDataGrid_RowCancelingEdit"
                    OnUpdateCommand="MyDataGrid_RowUpdating"
                    DataKeyField="OutageDetailId" 
                    Font-Names="Verdana">
                  <Columns>
                     <asp:CommandField ShowEditButton="True" EditText="Edit" CancelText="Cancel" UpdateText="Update" />
                 </Columns>
                <HeaderStyle BackColor="White"></HeaderStyle>
            </asp:GridView>

有人可以阐明我错过了什么吗?

点击编辑的那一刻,你去获取必须更新的行的 ID,然后从这一行获取它

Dim SOutageDetailId As String = MyDataGrid.Rows(e.RowIndex).Cells(0).Text

但是在页面加载时,您已设置

    If Not IsPostBack Then
        BindGrid()
    End If

因此,在回发时,直到您尝试从单元格获取 ID 为止的网格为空。

两种方式,以太币在回发时再次提供数据,并在更新后立即进行DataBind,或者获取网格视图的索引以进行更新,而不是获取单元格。

例如,我会将您的代码更改为:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)        
        BindGrid()
End Sub
Private Sub BindGrid()
    Dim dt As New DataTable()
    Dim connection As New SqlConnection("server='SQLEXPRESS'; trusted_connection='true'; Database='OutagesMgt_db'")
    Try
        connection.Open()
        Dim sqlStatement As String = "SELECT OutageDetailId, LocationName, Description, DetailDescription, CreateDate, StatusId FROM OutageDetail WHERE StatusId='1' ORDER BY CreateDate DESC"
        Dim cmd As New SqlCommand(sqlStatement, connection)
        Dim sqlDa As New SqlDataAdapter(cmd)
        sqlDa.Fill(dt)
        If dt.Rows.Count > 0 Then
            MyDataGrid.DataSource = dt
           If Not IsPostBack Then
               MyDataGrid.DataBind()
           End If                
        End If
    Catch ex As System.Data.SqlClient.SqlException
        Dim msg As String = "Fetch Error:"
        msg += ex.Message
        Throw New Exception(msg)
    Finally
        connection.Close()
    End Try
End Sub

[*] 假设您在 sql 上没有其他错误...

最新更新