Vb.net 如何在GridView的所有事件中获取DataTable

Vb.net 如何在GridView的所有事件中获取DataTable,vb.net,Vb.net,我的表单中有一个gridview。第一次在pageload中,我通过调用函数“FillgridFileExpenses()”加载gridview 在上面的代码中,iam使用名为dtInvoice的数据表。iam在gridview事件中所做的任何更改(如删除、更新或添加新行)都应反映在dtInvoice中,而不是反映在数据库中。iam在更新事件中获取dtInvoice,但在删除事件中获取dtInvoice dtInvoice没有行,出现错误。以下是我的更新和删除代码 Protected Sub G

我的表单中有一个gridview。第一次在pageload中,我通过调用函数“FillgridFileExpenses()”加载gridview

在上面的代码中,iam使用名为dtInvoice的数据表。iam在gridview事件中所做的任何更改(如删除、更新或添加新行)都应反映在dtInvoice中,而不是反映在数据库中。iam在更新事件中获取dtInvoice,但在删除事件中获取dtInvoice dtInvoice没有行,出现错误。以下是我的更新和删除代码

Protected Sub GridViewInvoiceT_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridViewInvoiceT.RowUpdating
    cmbChargeName = CType(GridViewInvoiceT.Rows(e.RowIndex).FindControl("cmbChargeName"), DropDownList)
    cmbInvCurency = CType(GridViewInvoiceT.Rows(e.RowIndex).FindControl("cmbInvCurency"), DropDownList)
    txtInvoiceNo1 = CType(GridViewInvoiceT.Rows(e.RowIndex).FindControl("txtInvoiceNo1"), TextBox)
    txtInvoiceDate1 = CType(GridViewInvoiceT.Rows(e.RowIndex).FindControl("txtInvoiceDate1"), TextBox)
    txtInvAmount1 = CType(GridViewInvoiceT.Rows(e.RowIndex).FindControl("txtInvAmount1"), TextBox)
    txtInvExRate1 = CType(GridViewInvoiceT.Rows(e.RowIndex).FindControl("txtInvExRate1"), TextBox)
    txtInvRemarks1 = CType(GridViewInvoiceT.Rows(e.RowIndex).FindControl("txtInvRemarks1"), TextBox)
    cmbAllinRight = CType(GridViewInvoiceT.Rows(e.RowIndex).FindControl("ddAllInRate"), DropDownList)
    Dim lblAllInRate As Label = CType(GridViewInvoiceT.Rows(e.RowIndex).FindControl("lblAllInRate"), Label)
    objinvoiceT.intfileID = Convert.ToInt32(txtInvFileNo.Text)
     dtInvoice.Rows(e.RowIndex).Item("AllInRate") = cmbAllinRight.SelectedItem
    dtInvoice.Rows(e.RowIndex).Item("ChargeName") = cmbChargeName.SelectedItem
    dtInvoice.Rows(e.RowIndex).Item("InvoiceNo") = txtInvoiceNo1.Text
    If txtInvoiceDate1.Text = String.Empty Then
        dtInvoice.Rows(e.RowIndex).Item("InvoiceDate") = Convert.ToDateTime("1/1/1800")
    Else
        dtInvoice.Rows(e.RowIndex).Item("InvoiceDate") = Convert.ToDateTime(txtInvoiceDate1.Text)
    End If
    dtInvoice.Rows(e.RowIndex).Item("Currency") = cmbInvCurency.SelectedItem
    dtInvoice.Rows(e.RowIndex).Item("InvAmount") = txtInvAmount1.Text
    dtInvoice.Rows(e.RowIndex).Item("InvExRate") = txtInvExRate1.Text
    dtInvoice.Rows(e.RowIndex).Item("InvRemarks") = txtInvRemarks1.Text
    GridViewInvoiceT.EditIndex = -1
    GridViewInvoiceT.DataSource = dtInvoice
    GridViewInvoiceT.DataBind()
    FillInvoice()

End Sub

Protected Sub GridViewInvoiceT_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridViewInvoiceT.RowDeleting
          dtInvoice.Rows.Remove(GridViewInvoiceT.DataKeys(e.RowIndex).Value)
    GridViewInvoiceT.DataSource = dtInvoice
    GridViewInvoiceT.DataBind()
End Sub
端接头 子发票()


任何人都可以帮助获取dtInvoice,以及我在删除中更新时所做的反射更改吗?

在您的GridViewInvoice\u行删除方法中,更改

dtInvoice.Rows.Remove(GridViewInvoiceT.DataKeys(e.RowIndex).Value)    
对数据行使用“Delete”方法

移除-移除datatable的行,使数据提供程序不再注意到该行。通过使用delete,您正在标记要删除的行,因此您的提供者代码将注意到删除,并请求从数据库中删除该行

    GridViewInvoiceT.DataSource = dtInvoice
    GridViewInvoiceT.DataBind()
End Sub
dtInvoice.Rows.Remove(GridViewInvoiceT.DataKeys(e.RowIndex).Value)