2012-10-06 40 views
1

我開發了以下用於編輯GridView的代碼(以下用C#編寫的教程),它進入編輯模式,但我的編輯不起作用,這裏是我的代碼:使用VB.Net使用SqlDatasource編輯Gridview w/o

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> 

有人能闡明什麼,我缺少的部分,請輕。

回答

0

你打了編輯的那一刻,你去獲取必須更新該行的ID,並從該行

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

得到它,但在頁面加載您已設置

If Not IsPostBack Then 
     BindGrid() 
    End If 

等回帖後,網格直到您嘗試從單元格獲取id的點爲空。

兩種方式,ether在post back上再次提供數據,並在更新後立即使DataBind,或者獲取Grid View的Index來進行Update,而不是Cell。

例如,我將你的代碼更改爲:

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其他錯誤...

+0

謝謝你,做到了,對於道歉遲迴復。再次感謝! – user1724708

相關問題