Editing in GridView Control

Reply

Join Date: Jun 2007
Posts: 23
Reputation: guptaalok12 is an unknown quantity at this point 
Solved Threads: 0
guptaalok12 guptaalok12 is offline Offline
Newbie Poster

Editing in GridView Control

 
0
  #1
Jun 12th, 2008
Here is my code i have shooping cart.i have created a table and bind this with GridView.Now i want to update this table through the GridView Edit Button(Suppose The Qty Field).How i will i do it.


  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3. Partial Class MyCart
  4. Inherits System.Web.UI.Page
  5. Dim DT As System.Data.DataTable
  6. Dim DR As DataRow
  7. Dim Match As Boolean = False
  8. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  9. If Not IsPostBack Then
  10. If Not Session("Cart") Is Nothing Then
  11. DT = Session("Cart")
  12. GridView1.DataSource = DT
  13. GridView1.DataBind()
  14. lbltotal.Text = TotalAmount(DT)
  15. lbltotalitem.Text = DT.Rows.Count & " Items"
  16. lbltotalitemprice.Text = TotalAmount(DT)
  17. dv.Visible = True
  18. Else
  19. dv.Visible = False
  20. End If
  21. End If
  22. End Sub
  23. Function TotalAmount(ByVal DT As DataTable) As Decimal
  24. Dim returntotal As Decimal
  25. For Each DR In DT.Rows
  26. returntotal += DR("saleprice") * DR("Qty")
  27. Next
  28. Return returntotal
  29. End Function
  30. Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
  31. DT = Session("Cart")
  32. DT.Rows(e.RowIndex).Delete()
  33. Session("Cart") = DT
  34. GridView1.DataSource = DT
  35. GridView1.DataBind()
  36. lbltotal.Text = TotalAmount(DT)
  37. End Sub
  38.  
  39. End Class

-------------------------------------------------------------------------
  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3. Partial Class ProductInformation
  4. Inherits System.Web.UI.Page
  5. Dim DT As DataTable
  6. Dim DR As DataRow
  7. Dim Match As Boolean = False
  8. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  9. If Not IsPostBack Then
  10. Dim Con As New SqlConnection
  11. Con.ConnectionString = "Data source=SOFT-D5F98EFF93;Initial Catalog=ECommerce;Integrated Security=True"
  12. Dim Com As New SqlCommand("Select * from Products Where ProductID='" + Request.QueryString("ID") + "'", Con)
  13. Dim DR As SqlDataReader
  14. Con.Open()
  15. DR = Com.ExecuteReader
  16. If DR.HasRows Then
  17. While DR.Read
  18. img1.ImageUrl = DR("Product Path")
  19. lblid.Text = DR("ProductID")
  20. lblisactive.Text = DR("IsActive")
  21. lblsaleprice.Text = DR("SalePrice")
  22. End While
  23. End If
  24. Con.Close()
  25. If Session("Cart") Is Nothing Then
  26. MakeCart()
  27. End If
  28.  
  29. End If
  30. End Sub
  31. Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  32. AddToCart()
  33. Dim str As String
  34. str = "<script language='javascript'>window.close();</script>"
  35. Response.Redirect("Close.aspx")
  36. End Sub
  37. Sub MakeCart()
  38. DT = New DataTable("MyCart")
  39. DT.Columns.Add("ID", GetType(Integer))
  40. DT.Columns.Add("SalePrice", GetType(Decimal))
  41. DT.Columns.Add("Qty", GetType(Integer))
  42. DT.Columns.Add("Total", GetType(Decimal))
  43. Session("Cart") = DT
  44. End Sub
  45. Sub AddToCart()
  46. DT = Session("Cart")
  47. MatchProduct(DT)
  48. If Not Match Then
  49. DR = DT.NewRow
  50. DR("ID") = lblid.Text
  51. DR("SalePrice") = Decimal.Parse(lblsaleprice.Text)
  52. DR("Qty") = txtqty1.Text
  53. DR("Total") = (Decimal.Parse(lblsaleprice.Text) * CType(txtqty1.Text, Integer))
  54. DT.Rows.Add(DR)
  55. Session("Cart") = DT
  56. End If
  57.  
  58. End Sub
  59. Function TotalAmount(ByVal DT As DataTable) As Decimal
  60. Dim RV As Decimal
  61. For Each DR In DT.Rows
  62. RV += DR("Qty") * DR("Saleprice")
  63. Next
  64. Return RV
  65. End Function
  66. Sub MatchProduct(ByVal DT As DataTable)
  67. For Each DR In DT.Rows
  68. If DR("ID") = lblid.Text Then
  69. DR("Qty") = txtqty1.Text
  70. Match = True
  71. Exit For
  72. End If
  73. Next
  74. End Sub
  75. End Class
Last edited by peter_budo; Jun 16th, 2008 at 9:36 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 8
Reputation: UIJAD is an unknown quantity at this point 
Solved Threads: 0
UIJAD UIJAD is offline Offline
Newbie Poster

Re: Editing in GridView Control

 
0
  #2
Jun 17th, 2008
protected sub WTVRNAMEHERE (ByVal sender as object, ByVal e as System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles GRIDNAME.RowUpdated

here you grab the row ( e.affectedrows) and apply the changes in the datatable ( which is the initial datasource... all that of course after adding the "Edit/Update" button to your gridview

End Sub
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the ASP.NET Forum


Views: 2591 | Replies: 1
Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC