I have a page that contains 2 dropdowns, 2 textboxes and a Button. The user will select items from the dropdowns and then key in data into the textboxes. After doing so, they will click a button to take the information from these controls and populate an "Order Container". They will be able to enter multiple "Orders."
Thanks for helping! Mike
Update: Here's how I'm updating the gridview:
Protected Sub imgAddOrderItemClick(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgAddOrderItem.Click
Dim qty As String 'Integer
Dim type As String
Dim product As String
Dim price As Integer
Dim count As Integer
count = GridView1.Rows.Count
type = ddlProductTypes.SelectedItem.ToString
product = ddlProductFamilies.SelectedItem.ToString
price = 11
qty = TextBox10.Text
' Populate the datatable with your data (put this in appropriate loop)
dr = dt.NewRow
dr("Type") = type
dr("Product") = product
dr("Qty") = qty
dr("Price") = price
' Add the row
dt.Rows.Add(dr)
dt.AcceptChanges()
GridView1.DataSource = dt 'GetData()
GridView1.DataBind()
End Sub
GridView is fine !
Where is your dt coming from?
I think the problem is that when you post back, your dt gets initialised and therefore it is empty. That is why you only get a one (new) record every time. There are two ways to sort it,
(1). You have to keep dt (or data source) in your session and your code is fine.
(2). If dt is not in session, you need to first loop through the gridview rows and columns to fill already added data (if any), next add the new order and finally bind it to the gridview.
Hope that helps!
The Gridview will work just fine. You are seeing your row overwritten most likely because your data table ( dt
) in your code is being reinstantiated on every request. What you need to do is keep that table in memory (put it in Session, for example), grab it from there, add the new row and rebind the GridView. Something like this:
Protected Sub imgAddOrderItemClick(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgAddOrderItem.Click
dt = Session("Data")
If dt is Nothing Then
' Create your DT columns here
Session.Add("Data",dt)
End If
'Add rows here and rebind
dr = dt.NewRow
dr("Type") = type
dr("Product") = product
dr("Qty") = qty
dr("Price") = price
' Add the row
dt.Rows.Add(dr)
dt.AcceptChanges()
GridView1.DataSource = dt 'GetData()
GridView1.DataBind()
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.