删除行/删除行

时间:2017-12-14 10:35:01

标签: vb.net gridview rows

我想在gridview中删除我的行,但是当我尝试很多代码时,错误是相同的。

当我尝试这个时

Using sqlCon As New SqlConnection(PyrDLL.Koneksi.ConnectionString)
        Using cmd As New SqlCommand()
            cmd.CommandText = "xyz"
            cmd.Connection = sqlCon
            sqlCon.Open()
            Dim da As New SqlDataAdapter(cmd)
            Dim dt As New DataTable()
            da.Fill(dt)
            Gridview1.DataSource = dt
            Gridview1.DataBind()
            sqlCon.Close()
            If Gridview1.Rows.Count = 0 Then
                Dim dtempty As DataTable = Nothing
                dtempty = (DirectCast(Gridview1.DataSource, DataTable)).Clone()
                dtempty.Rows.Add(dtempty.NewRow())
                Gridview1.DataSource = dtempty
                Gridview1.DataBind()
                Gridview1.Rows(0).Visible = False
                'Gridview1.Rows(0).Controls.Clear()
            Else
                For i As Integer = 0 To dt.Rows.Count - 1
                    dt.Rows(i)("rupiah") = PyrDLL.Decrypt(dt.Rows(i)("rupiah"))
                    dt.Rows(i)("rupiah") = Decimal.Parse(dt.Rows(i)("rupiah")).ToString()
                    'i = i + 1
                Next
                Gridview1.DataBind()
                For i As Integer = Gridview1.Rows.Count - 1 To 1 Step -1
                    Dim row As GridViewRow = Gridview1.Rows(i)
                    Dim prevrow As GridViewRow = Gridview1.Rows(i - 1)
                    If (TryCast(Gridview1.Rows(i).Cells(1).FindControl("lblketerangan"), Label).Text.ToString() = TryCast(Gridview1.Rows(i - 1).Cells(1).FindControl("lblketerangan"), Label).Text.ToString()) And (TryCast(Gridview1.Rows(i).Cells(0).FindControl("lblcomp"), Label).Text.ToString() = TryCast(Gridview1.Rows(i - 1).Cells(0).FindControl("lblcomp"), Label).Text.ToString()) Then
                        Dim total As Integer = Convert.ToDecimal(TryCast(Gridview1.Rows(i - 1).Cells(2).FindControl("lblrupiah"), Label).Text)
                        Dim total2 As Integer = Convert.ToDecimal(TryCast(Gridview1.Rows(i).Cells(2).FindControl("lblrupiah"), Label).Text)
                        Dim total3 As Decimal
                        total3 = total + total2
                        DirectCast(Gridview1.Rows(i - 1).Cells(2).FindControl("lblrupiah"), Label).Text = Decimal.Parse(total3).ToString()
                        row.Visible = False
                        'Gridview1.Rows.Remove(Gridview1.Rows(i)) <--- if i comment here its run without problem
                    End If
                Next
            End If
        End Using
    End Using

错误

删除'不是'System.Web.UI.WebControls.GridViewRowCollection'的成员

你能帮助我吗?

现在,我总结之后隐藏了这行但是我想在我总结之后自动删除/删除行不仅隐藏

由于

2 个答案:

答案 0 :(得分:0)

我不确定当没有行时你想要做什么,gridview将是空的

您应该执行以下操作:

'Call GetData() every time you want to bind data to the gridview
Private Function GetData()

    Gridview1.DataSource = Nothing
    Gridview1.DataBind()

    Dim strQuery = "SELECT..."
    Dim dt As New DataTable()

    Using sqlCon As New SqlConnection(PyrDLL.Koneksi.ConnectionString)
        Using cmd As New SqlCommand(strQuery, sqlCon)
        'Add cmd parameters as required to prevent SqlInjection
        sqlCon.Open()

            Using ada As New SqlDataAdapter(cmd)
                ada.Fill(dt)
                Gridview1.DataSource = dt
                Gridview1.DataBind()
            End Using

        End Using
    End Using
End Function

当处理gridview中的数据时,您可以使用RowDataBound,然后您可以调用一个单独的函数来处理在处理逻辑后删除行,其中符合条件以删除行

Protected Sub Gridview1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles Gridview1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        'Handle databound logic
    End If
End Sub

每次需要将数据重新绑定到gridview时,您应该能够根据需要定制它,只需调用GetData()

答案 1 :(得分:0)

我解决了我的问题 我无法从数据gridview中删除行,但我从数据表中删除数据并在之后绑定它 谢谢大家