2014年7月13日 星期日

[RESOLVED] Help with grid view and dataList


Hello. I need help with a part of my website im coding. I have a listBox_Products which is populated with sqlServerDataSource. When i click on a product, It will display a corresponding picture along a gridview with productPrice and productName. The gridview
is databound programmatacally in the codebehind, on the indexChange event. I have a button, addToShoppingCart. On the button_Click event, i want to add the item the user chose to the shopping cart(its on the same page), i have a second gridView, which received
the item the user chose and displays it. this is where im stuck. i know i cant append to gridview, and i know it has to be databound. My logic is this: get the first item user chooses, add it to a datatable, insert into second gridview. now if the user chooses
another product, same logic, except this time, i would add a new row, and add the new data to the new row, keeping old row as well. problem is i cant figure out how to do this. im not very experienced in datatable. 


this is my code behind.


'this is my code to get the product from sql server


Private Sub GetProducts()

Dim TechShopConnectionString As String = ConfigurationManager.ConnectionStrings("Zapata_IT_DataBaseConnectionString").ConnectionString

Dim TechCon As New SqlConnection(TechShopConnectionString)

Dim TechCmd As New SqlCommand()


Dim index As Integer = ListBox_Products.SelectedIndex()


TechCmd.CommandType = CommandType.StoredProcedure

TechCmd.CommandText = "GetAllProductInformationByID"

TechCmd.Parameters.Add("@ProductID", SqlDbType.Int).Value = index

TechCmd.Connection = TechCon


Try

TechCon.Open()

GridView2.EmptyDataText = "No Records Found"

GridView2.DataSource = TechCmd.ExecuteReader()

GridView2.DataBind()


Catch ex As Exception

Throw ex

Finally

TechCon.Close()

TechCon.Dispose()

End Try

End Sub


'this is my code to add item to the second gridview


Protected Sub Button_AddToCart_Click(sender As Object, e As EventArgs) Handles Button_AddToCart.Click

Dim conn As SqlConnection = Nothing

Dim index As Integer = ListBox_Products.SelectedIndex()


Try

Dim connString As String = ConfigurationManager.ConnectionStrings("Zapata_IT_DataBaseConnectionString").ConnectionString

conn = New SqlConnection(connString)

Dim cmd As SqlCommand = New SqlCommand()


cmd.CommandType = CommandType.StoredProcedure

cmd.CommandText = "GetAllProductInformationByID"

cmd.Parameters.Add("@ProductID", SqlDbType.Int).Value = index

cmd.Connection = conn


conn.Open()

Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

Dim dr2 As DataRow


dataTableCheckOut.NewRow()

dataTableCheckOut.Load(dr, LoadOption.OverwriteChanges)


GridView_CheckOut.DataSource = dataTableCheckOut

GridView_CheckOut.DataBind()


Catch ex As SqlException


Catch ex As Exception


Finally

conn.Close()

End Try



as you can see im pretty lost. i know the logic is fine, i cant figure out the code.


any help would be appreciated.



Hi uzii,


From my understanding, is that you would like to rember the olde row of
GridView
fater add a new row.


Because the GridView can't rember the old value each reload, so I think, maybe we can use the
ViewState["oldrow"] = OldRow to remember your old row.


And then we can get the old row from ViewState, then add it to
GridView.


Hope it helps.


Best Regards,

Terry Guo


沒有留言:

張貼留言