| | |
Editing in GridView Control
Please support our ASP.NET advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2007
Posts: 23
Reputation:
Solved Threads: 0
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.
-------------------------------------------------------------------------
asp.net Syntax (Toggle Plain Text)
Imports System.Data Imports System.Data.SqlClient Partial Class MyCart Inherits System.Web.UI.Page Dim DT As System.Data.DataTable Dim DR As DataRow Dim Match As Boolean = False Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then If Not Session("Cart") Is Nothing Then DT = Session("Cart") GridView1.DataSource = DT GridView1.DataBind() lbltotal.Text = TotalAmount(DT) lbltotalitem.Text = DT.Rows.Count & " Items" lbltotalitemprice.Text = TotalAmount(DT) dv.Visible = True Else dv.Visible = False End If End If End Sub Function TotalAmount(ByVal DT As DataTable) As Decimal Dim returntotal As Decimal For Each DR In DT.Rows returntotal += DR("saleprice") * DR("Qty") Next Return returntotal End Function Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting DT = Session("Cart") DT.Rows(e.RowIndex).Delete() Session("Cart") = DT GridView1.DataSource = DT GridView1.DataBind() lbltotal.Text = TotalAmount(DT) End Sub End Class
-------------------------------------------------------------------------
asp.net Syntax (Toggle Plain Text)
Imports System.Data Imports System.Data.SqlClient Partial Class ProductInformation Inherits System.Web.UI.Page Dim DT As DataTable Dim DR As DataRow Dim Match As Boolean = False Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then Dim Con As New SqlConnection Con.ConnectionString = "Data source=SOFT-D5F98EFF93;Initial Catalog=ECommerce;Integrated Security=True" Dim Com As New SqlCommand("Select * from Products Where ProductID='" + Request.QueryString("ID") + "'", Con) Dim DR As SqlDataReader Con.Open() DR = Com.ExecuteReader If DR.HasRows Then While DR.Read img1.ImageUrl = DR("Product Path") lblid.Text = DR("ProductID") lblisactive.Text = DR("IsActive") lblsaleprice.Text = DR("SalePrice") End While End If Con.Close() If Session("Cart") Is Nothing Then MakeCart() End If End If End Sub Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click AddToCart() Dim str As String str = "<script language='javascript'>window.close();</script>" Response.Redirect("Close.aspx") End Sub Sub MakeCart() DT = New DataTable("MyCart") DT.Columns.Add("ID", GetType(Integer)) DT.Columns.Add("SalePrice", GetType(Decimal)) DT.Columns.Add("Qty", GetType(Integer)) DT.Columns.Add("Total", GetType(Decimal)) Session("Cart") = DT End Sub Sub AddToCart() DT = Session("Cart") MatchProduct(DT) If Not Match Then DR = DT.NewRow DR("ID") = lblid.Text DR("SalePrice") = Decimal.Parse(lblsaleprice.Text) DR("Qty") = txtqty1.Text DR("Total") = (Decimal.Parse(lblsaleprice.Text) * CType(txtqty1.Text, Integer)) DT.Rows.Add(DR) Session("Cart") = DT End If End Sub Function TotalAmount(ByVal DT As DataTable) As Decimal Dim RV As Decimal For Each DR In DT.Rows RV += DR("Qty") * DR("Saleprice") Next Return RV End Function Sub MatchProduct(ByVal DT As DataTable) For Each DR In DT.Rows If DR("ID") = lblid.Text Then DR("Qty") = txtqty1.Text Match = True Exit For End If Next End Sub End Class
Last edited by peter_budo; Jun 16th, 2008 at 9:36 am. Reason: Keep It Organized - please use [code] tags
•
•
Join Date: May 2008
Posts: 8
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- MultiLine TextBoxes in GridView edit mode (ASP.NET)
- Validator in GridView (ASP.NET)
Other Threads in the ASP.NET Forum
- Previous Thread: HtmlTextWriter
- Next Thread: Common Chatting in asp.net
Views: 2591 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 ajax alltypeofvideos anathor appliances application asp asp.net beginner box browser button c# cac checkbox commonfunctions complex connection control dataaccesslayer database datagridview datalist deployment development dgv dialog dropdownlist dynamic dynamically edit editing embeddingactivexcontrol expose feedback fileuploader fill findcontrol flash form formatdecimal formview gridview gudi iis image javascript languages list listbox login microsoft mobile mouse mssql news novell numerical opera panelmasterpagebuttoncontrols parent problem project radio redirect registration relationaldatabases reportemail response.redirect richtextbox rows schoolproject search security select services sessionvariables silverlight smoobjects software sql sql-server sqlserver2005 ssl theft tracking treeview validatedate validation vb.net videos vista visualstudio web webapplications webdevelopment webprogramming webservice xsl





