/ / Як отримати таблицю даних у всіх подіях GridView - vb.net

Як отримати таблицю даних у всіх подіях GridView - vb.net

У моїй формі є gridview.FirstTime при завантаженні сторінки iam, що завантажує gridview, викликаючи функцію "FillgridFileExpenses ()".

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

У наведеному вище коді iam використовується таблиця даних, яку називаютьdtInvoice. Незалежно від змін, внесених iam у події gridview, таких як видалення або оновлення або додавання нового рядка, все це повинно відображатися в 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

Кінець Sub Sub FillInvoice ()

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

Хто-небудь може допомогти отримати dtInvoice із відображеними змінами, які я вніс в оновлення при видаленні?

Відповіді:

1 для відповіді № 1

У вашому методі GridViewInvoiceT_RowDeleting змініть;

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

використовувати метод "Видалити" в DataRow.

Видалити - видаляє рядок для таблиці даних, через що постачальник даних більше не помічає рядка. Використовуючи видалення, ви є маркування рядок, який потрібно видалити, отже, код постачальника помітить видалення та попросить видалити рядок з бази даних.