使用VB.Net使用SqlDatasource编辑Gridview

时间:2012-10-06 07:46:01

标签: asp.net vb.net gridview editing

我开发了以下用于编辑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>

请有人了解我所缺少的内容。

1 个答案:

答案 0 :(得分:0)

当您点击编辑时,您将获得必须更新的行的ID,并从此行获取

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

但是在页面加载时你已经设置了

    If Not IsPostBack Then
        BindGrid()
    End If

所以在帖子后面,网格到达你试图从单元格获取id的点是空的。

两种方式,ether再次提供回发后的数据,并在更新后立即生成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上没有其他错误...