簡體   English   中英

GridView的所有事件如何獲取DataTable

[英]How To Get DataTable In All Events OF GridView

我的表單中有一個 gridview。第一次在頁面加載中通過調用 function“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 中而不是數據庫中。我在更新事件中獲取 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 嗎?

在您的 GridViewInvoiceT_RowDeleting 方法中,更改;

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

在 DataRow 上使用“刪除”方法。

刪除 - 刪除數據表的行,使您的數據提供者不再注意到該行。 通過使用 delete,您將標記要刪除的行,因此您的提供程序代碼會注意到刪除,並請求從數據庫中刪除該行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM