0

当我从 gridview 中删除一行时,它不会删除选择要删除的行,而是删除 gridview 的最后一行。它还会清除现在是最后一行的行的用户输入,但所有其他行都保留它们的输入。非常感谢您对此的任何帮助。

这是我的 RowDeleting 子:

Protected Sub gvPDetails_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvPDetails.RowDeleting

    If ViewState("CurrentTable") IsNot Nothing Then
        Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
        Dim delRowIndex As Integer = Convert.ToInt32(e.RowIndex)
        Dim delRow As DataRow = dt.Rows(delRowIndex)
        dt.Rows.Remove(delRow)
        SetRowData()
        ViewState("CurrentTable") = dt
    End If
End Sub

这是 SetRowData()

Private Sub SetRowData()

    Dim rowIndex As Integer = 0

    Dim arrl1 As New ArrayList()
    Dim arrl2 As New ArrayList()
    Dim arrl3 As New ArrayList()

    If ViewState("CurrentTable") IsNot Nothing Then
        Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
        Dim drCurrentRow As DataRow = Nothing

        If dtCurrentTable.Rows.Count > 0 Then

            For i As Integer = 0 To dtCurrentTable.Rows.Count - 1
                //extract the TextBox values 
                Dim box1 As TextBox = DirectCast(gvPDetails.Rows(rowIndex).FindControl("txtStore"), TextBox)
                //extract the DropDownList Selected Items 
                Dim ddl1 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlStatus"), DropDownList)
                Dim ddl2 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlReason"), DropDownList)
                Dim ddl3 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlPldDetail"), DropDownList)
                Dim box2 As TextBox = DirectCast(gvPDetails.Rows(rowIndex).FindControl("txtInfo"), TextBox)

                //Update the DataRow with the DDL Selected Items 
                dtCurrentTable.Rows(rowIndex)("Store") = box1.Text
                dtCurrentTable.Rows(rowIndex)("Status") = ddl1.SelectedItem.Text
                myDD1 = ddl1.SelectedItem.Text
                dtCurrentTable.Rows(rowIndex)("Reason") = ddl2.SelectedItem.Text
                myDD2 = ddl2.SelectedItem.Text
                If ddl3 IsNot Nothing And ddl3.Enabled = True Then
                    dtCurrentTable.Rows(rowIndex)("Detail") = ddl3.SelectedItem.Text
                End If

                myDD3 = ddl3.SelectedItem.Text
                dtCurrentTable.Rows(rowIndex)("Information") = box2.Text

                rowIndex += 1

                Dim MemErrList As New ArrayList()
                Dim index As Integer = -1
                Dim row As GridViewRow = gvPDetails.Rows(i)
                index = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
                Dim result As Boolean = DirectCast(gvPDetails.Rows(i).FindControl("ckbMemErr"), CheckBox).Checked

                //Check in the Session
                If Session("CHECKED_ITEMS") IsNot Nothing Then
                    MemErrList = DirectCast(Session("CHECKED_ITEMS"), ArrayList)
                End If
                If result Then
                    If Not MemErrList.Contains(index) Then
                        MemErrList.Add(index)
                    End If
                Else
                    MemErrList.Remove(index)
                End If
                //Next
                If MemErrList IsNot Nothing AndAlso MemErrList.Count > 0 Then
                    Session("CHECKED_ITEMS") = MemErrList
                End If

                //----SLS File Val Checkbox Array Builder-----------------------------
                Dim DetailList As New ArrayList()
                Dim index1 As Integer = -1
                Dim row1 As GridViewRow = gvPDetails.Rows(i)
                index1 = CInt(gvPDetails.DataKeys(row1.RowIndex).Value)
                Dim result1 As Boolean = DirectCast(gvPDetails.Rows(i).FindControl("cbSLSFileVal"), CheckBox).Checked

                //Check in the Session
                If Session("DETAIL1_ITEMS") IsNot Nothing Then
                    DetailList = DirectCast(Session("DETAIL1_ITEMS"), ArrayList)
                End If
                If result1 Then
                    If Not DetailList.Contains(index1) Then
                        DetailList.Add(index1)
                    End If
                Else
                    DetailList.Remove(index1)
                End If
                //Next
                If DetailList IsNot Nothing AndAlso DetailList.Count > 0 Then
                    Session("DETAIL1_ITEMS") = DetailList
                End If

                //----Router Checkbox Array Builder-----------------------------
                Dim DetailList2 As New ArrayList()
                Dim index2 As Integer = -1
                Dim row2 As GridViewRow = gvPDetails.Rows(i)
                index2 = CInt(gvPDetails.DataKeys(row2.RowIndex).Value)
                Dim result2 As Boolean = DirectCast(gvPDetails.Rows(i).FindControl("cbRouter"), CheckBox).Checked

                //Check in the Session
                If Session("DETAIL2_ITEMS") IsNot Nothing Then
                    DetailList2 = DirectCast(Session("DETAIL2_ITEMS"), ArrayList)
                End If
                If result2 Then
                    If Not DetailList2.Contains(index2) Then
                        DetailList2.Add(index2)
                    End If
                Else
                    DetailList2.Remove(index2)
                End If
                //Next
                If DetailList2 IsNot Nothing AndAlso DetailList2.Count > 0 Then
                    Session("DETAIL2_ITEMS") = DetailList2
                End If

                //----Register1 Checkbox Array Builder-----------------------------
                Dim DetailList3 As New ArrayList()
                Dim index3 As Integer = -1
                Dim row3 As GridViewRow = gvPDetails.Rows(i)
                index3 = CInt(gvPDetails.DataKeys(row3.RowIndex).Value)
                Dim result3 As Boolean = DirectCast(gvPDetails.Rows(i).FindControl("cbRegister1"), CheckBox).Checked

                //Check in the Session
                If Session("DETAIL3_ITEMS") IsNot Nothing Then
                    DetailList3 = DirectCast(Session("DETAIL3_ITEMS"), ArrayList)
                End If
                If result3 Then
                    If Not DetailList3.Contains(index3) Then
                        DetailList3.Add(index3)
                    End If
                Else
                    DetailList3.Remove(index3)
                End If
                //Next
                If DetailList3 IsNot Nothing AndAlso DetailList3.Count > 0 Then
                    Session("DETAIL3_ITEMS") = DetailList3
                End If

                //----Register2 Checkbox Array Builder-----------------------------
                Dim DetailList4 As New ArrayList()
                Dim index4 As Integer = -1
                Dim row4 As GridViewRow = gvPDetails.Rows(i)
                index4 = CInt(gvPDetails.DataKeys(row4.RowIndex).Value)
                Dim result4 As Boolean = DirectCast(gvPDetails.Rows(i).FindControl("cbRegister2"), CheckBox).Checked

                //Check in the Session
                If Session("DETAIL4_ITEMS") IsNot Nothing Then
                    DetailList4 = DirectCast(Session("DETAIL4_ITEMS"), ArrayList)
                End If
                If result4 Then
                    If Not DetailList4.Contains(index4) Then
                        DetailList4.Add(index4)
                    End If
                Else
                    DetailList4.Remove(index4)
                End If
                //Next
                If DetailList4 IsNot Nothing AndAlso DetailList4.Count > 0 Then
                    Session("DETAIL4_ITEMS") = DetailList4
                End If
                arrl1.Add(myDD1)
                arrl2.Add(myDD2)
                arrl3.Add(myDD3)
            Next
            //dtCurrentTable.Rows.Add(drCurrentRow)
            ViewState("CurrentTable") = dtCurrentTable

            //Rebind the Grid with the current data to reflect changes 
            gvPDetails.DataSource = dtCurrentTable
            gvPDetails.DataBind()
        End If
    Else

    End If
    //Set Previous Data on Postbacks 
    SetPrevDataOnDelete(arrl1, arrl2, arrl3)
End Sub

这是 SetPrevDataOnDelete(arrl1, arrl2, arrl3)

Private Sub SetPrevDataOnDelete(ByVal arrl1 As ArrayList, ByVal arrl2 As ArrayList, ByVal arrl3 As ArrayList)
    Dim rowIndex As Integer = 0
    myGridViewRow = 0
    If ViewState("CurrentTable") IsNot Nothing Then

        Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
        If dt.Rows.Count > 0 Then
            Dim arr1 As ArrayList = arrl1
            Dim arr2 As ArrayList = arrl2
            Dim arr3 As ArrayList = arrl3

            For i As Integer = 0 To dt.Rows.Count - 1

                Dim box1 As TextBox = DirectCast(gvPDetails.Rows(rowIndex).FindControl("txtStore"), TextBox)
                Dim ddl1 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlStatus"), DropDownList)
                Dim ddl2 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlReason"), DropDownList)
                Dim ddl3 As DropDownList = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ddlPldDetail"), DropDownList)
                Dim box2 As TextBox = DirectCast(gvPDetails.Rows(rowIndex).FindControl("txtInfo"), TextBox)
                Dim cbResult As CheckBox = DirectCast(gvPDetails.Rows(rowIndex).FindControl("ckbMemErr"), CheckBox)


                //Fill the DropDownList with Data
                myGridViewRow = i
                //FillDropDownList(ddl1)
                FillDDL2OnDel(arrl1, rowIndex)
                FillDDL3OnDel(arrl1, arrl2, rowIndex)

                If i < dt.Rows.Count - 1 Then
                    //Assign the value from DataTable to the TextBox 
                    //gvPDetails.Rows(i).Cells(0).Text = Convert.ToString(i + 1)
                    box1.Text = dt.Rows(rowIndex)("Store").ToString()
                    box2.Text = dt.Rows(rowIndex)("Information").ToString()

                    ddl1.Items.FindByText(dt.Rows(rowIndex)("Status").ToString()).Selected = True
                    If ddl2 Is Nothing Then

                    ElseIf ddl2 Is Nothing Then

                    ElseIf ddl2 IsNot Nothing And ddl2.Enabled = True Then
                        ddl2.Items.FindByText(dt.Rows(rowIndex)("Reason").ToString()).Selected = True
                    End If

                    If ddl3 IsNot Nothing And ddl3.Enabled = True Then
                        ddl3.Items.FindByText(dt.Rows(rowIndex)("Detail").ToString()).Selected = True
                    End If


                    Dim MemErrList As ArrayList = DirectCast(Session("CHECKED_ITEMS"), ArrayList)
                    If MemErrList IsNot Nothing AndAlso MemErrList.Count > 0 Then
                        Dim row As GridViewRow = gvPDetails.Rows(i)
                        Dim index As Integer = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
                        If MemErrList.Contains(index) Then
                            Dim myCheckBox As CheckBox = DirectCast(row.FindControl("ckbMemErr"), CheckBox)
                            myCheckBox.Checked = True
                        End If
                        //Next
                    End If

                    Dim DetailList As ArrayList = DirectCast(Session("DETAIL1_ITEMS"), ArrayList)
                    If DetailList IsNot Nothing AndAlso DetailList.Count > 0 Then
                        Dim row As GridViewRow = gvPDetails.Rows(i)
                        Dim index1 As Integer = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
                        If DetailList.Contains(index1) Then
                            Dim myCheckBox1 As CheckBox = DirectCast(row.FindControl("cbSLSFileVal"), CheckBox)
                            myCheckBox1.Checked = True
                        End If
                        //Next
                    End If

                    Dim DetailList2 As ArrayList = DirectCast(Session("DETAIL2_ITEMS"), ArrayList)
                    If DetailList2 IsNot Nothing AndAlso DetailList2.Count > 0 Then
                        Dim row As GridViewRow = gvPDetails.Rows(i)
                        Dim index2 As Integer = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
                        If DetailList2.Contains(index2) Then
                            Dim myCheckBox2 As CheckBox = DirectCast(row.FindControl("cbRouter"), CheckBox)
                            myCheckBox2.Checked = True
                        End If
                        //Next
                    End If

                    Dim DetailList3 As ArrayList = DirectCast(Session("DETAIL3_ITEMS"), ArrayList)
                    If DetailList3 IsNot Nothing AndAlso DetailList3.Count > 0 Then
                        Dim row As GridViewRow = gvPDetails.Rows(i)
                        Dim index3 As Integer = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
                        If DetailList3.Contains(index3) Then
                            Dim myCheckBox3 As CheckBox = DirectCast(row.FindControl("cbRegister1"), CheckBox)
                            myCheckBox3.Checked = True
                        End If
                        //Next
                    End If

                    Dim DetailList4 As ArrayList = DirectCast(Session("DETAIL4_ITEMS"), ArrayList)
                    If DetailList4 IsNot Nothing AndAlso DetailList4.Count > 0 Then
                        Dim row As GridViewRow = gvPDetails.Rows(i)
                        Dim index4 As Integer = CInt(gvPDetails.DataKeys(row.RowIndex).Value)
                        If DetailList4.Contains(index4) Then
                            Dim myCheckBox4 As CheckBox = DirectCast(row.FindControl("cbRegister2"), CheckBox)
                            myCheckBox4.Checked = True
                        End If
                        //Next
                    End If

                End If
                rowIndex += 1
            Next
        End If
    End If
End Sub

再次感谢任何帮助。

4

1 回答 1

0

e.RowIndex 是否返回您期望的正确行号?

于 2013-06-04T19:51:02.903 回答