1

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>

誰かが私が見逃しているものに光を当てることができますか.

4

1 に答える 1

0

Edit を押した瞬間に、更新する必要がある行の ID を取得し、この行から ID を取得します。

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

しかし、設定したページの読み込み時に

    If Not IsPostBack Then
        BindGrid()
    End If

そのため、ポストバックでは、セルからIDを取得しようとするポイントまでのグリッドは空です。

2 つの方法として、ポストバック時にデータを再度提供し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 に他のバグがないと仮定すると...

于 2012-10-06T08:16:02.367 に答える