Reputation: 4319
I have a gridview in my form.FirstTime in pageload iam loading gridview by calling function "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
In the above code iam using a datatable called dtInvoice.Whatever changes iam making in gridview events such as deleting or updating or adding a new row,all these should reflect in dtInvoice and not in database.Iam getting dtInvoice in updating event ,but in deleting dtInvoice is coming with no rows and getting an error.Following is my code for updating and deleting
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
Can anyone help to get dtInvoice with reflectedchanges that i made in updating in deleting?
Upvotes: 0
Views: 1590
Reputation: 4201
In your GridViewInvoiceT_RowDeleting method, change;
dtInvoice.Rows.Remove(GridViewInvoiceT.DataKeys(e.RowIndex).Value)
to use the 'Delete' method on the DataRow.
Remove - removes the row for the datatable, causing you data provider not to notice the row anymore. By using delete, you are marking the row to be deleted, hence your provider code will notice the delete, and request the row to be deleted from the database.
Upvotes: 1