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

时间:2009-03-20 10:18:39

标签: vb.net

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

Sub FillgridFileExpenses()
        If txtInvFileNo.Text = String.Empty Then
            objinvoiceT.intfileID = 0
        Else
            objinvoiceT.intfileID = Convert.ToInt32(txtInvFileNo.Text)
        End If
        temptab1 = objinvoiceT.selFileExpensesView()
        temptab1.Columns.Add("AllInRate")
    If (temptab1.Rows.Count > 0) Then
        GridViewInvoiceT.DataSource = temptab1
        dtInvoice = temptab1
        'dtInvoice.Columns.Add("AllInRate")
        GridViewInvoiceT.DataBind()
        GridViewInvoiceT.Columns.Item(10).Visible = True
    Else
        temptab1.Rows.Add(temptab1.NewRow())
        GridViewInvoiceT.DataSource = temptab1
        dtInvoice = temptab1
        GridViewInvoiceT.DataBind()
        Dim TotalColumns As Integer
        TotalColumns = GridViewInvoiceT.Rows(0).Cells.Count
        GridViewInvoiceT.Rows(0).Cells.Clear()
        GridViewInvoiceT.Rows(0).Cells.Add(New TableCell())
        GridViewInvoiceT.Rows(0).Cells(0).ColumnSpan = TotalColumns
        If txtInvFileNo.Text = String.Empty Then
            GridViewInvoiceT.Rows(0).Cells(0).Text = "No Record Found, Must Select a File Number "
        Else
            GridViewInvoiceT.Rows(0).Cells(0).Text = "No Record Found For File Number : " + " " + txtInvFileNo.Text
        End If
    End If
End Sub

在上面的代码中,我使用了一个名为dtInvoice的数据表。无论我在gridview事件中做了什么改变,比如删除或更新或添加一个新行,所有这些都应该反映在dtInvoice而不是data.Iam得到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

End Sub     Sub FillInvoice()

    GridViewInvoiceT.DataSource = dtInvoice
    GridViewInvoiceT.DataBind()
End Sub

任何人都可以通过我在删除时更新所做的反映变化来帮助获取dtInvoice吗?

1 个答案:

答案 0 :(得分:1)

在GridViewInvoiceT_RowDeleting方法中,更改;

dtInvoice.Rows.Remove(GridViewInvoiceT.DataKeys(e.RowIndex).Value)    

在DataRow上使用'删除'方法。

删除 - 删除数据表的行,导致数据提供者不再注意该行。通过使用delete,您标记要删除的行,因此您的提供者代码会注意到删除,并请求从数据库中删除该行。