 |
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

November 29th, 2007, 02:15 PM
|
Authorized User
|
|
Join Date: Jun 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Problem with Adding a new row to an existing datag
Hello,
The existing data table binded to a grid works When I enter the number of line items I need and click on a button that say make grid. The problem is when click on Add Rows the extra row does not appear. I think I am having a problem with the databind, because if I use, gvParts.DataBind(), the existing datagrid goes away.
Thank you in advance,
Existing Code used to create the data table to add records (WORKS)
************************************************** ********
Protected Sub btnMkGrid_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnMkGrid.Click
Dim dt As New Data.DataTable
'Dim tblReceiving As Table
dt.Columns.Add(New Data.DataColumn("LineNO"))
For i As Integer = 1 To tbItems.Text
Dim dr As Data.DataRow
dr = dt.NewRow
dr("LineNO") = i
dt.Rows.Add(dr)
Next
'Bind the DataTable to the DataGrid
gvParts.DataSource = dt
gvParts.DataBind()
End Sub
************************************************** **
Does Not Work
Protected Sub AddRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddRow.Click
'Add the number of rows specified by "RowCount" to gvParts
If (IsNumeric(RowCount.Text)) Then
Dim i As Integer
For i = 1 To CInt(RowCount.Text)
'Adding a blank record to the dataset.
Dim dt As New Data.DataTable
dt.Columns.Add(New Data.DataColumn("LineNO"))
Dim dr As Data.DataRow
dr = dt.NewRow
dr("LineNO") = i
dt.Rows.Add(dr)
Next
End If
'Rebind the grid to show the newly added row(s).
'gvParts.DataBind()
'DataBind()
End Sub
|

November 29th, 2007, 02:36 PM
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
Your logic is all messed up. First, if your for loop executes more than once, the data from precceding loop is lost since your recreate the DataTable with every pass.
Second, you never specifiy a datasource for your grid. Your code should be something like:
Code:
Dim dt As New Data.DataTable
dt.Columns.Add(New Data.DataColumn("LineNO"))
If (IsNumeric(RowCount.Text)) Then
Dim i As Integer
For i = 1 To CInt(RowCount.Text)
Dim dr As Data.DataRow
dr = dt.NewRow
dr("LineNO") = i
dt.Rows.Add(dr)
Next
End If
'Rebind the grid to show the newly added row(s).
gvParts.DataSource = dt
gvParts.DataBind()
Issues: if the for never executes (or the IF statement for that matter), an empty DataTable will be bound to your Grid and erase all of your data. You could of course move the DataTable declaration into the IF as well as the code to specifiy the DataSource and DataBind.
Your code claims to add the table to a dataset, but I do not see a Dataset anywhere.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
|

November 29th, 2007, 04:00 PM
|
Authorized User
|
|
Join Date: Jun 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I am working on the logic, because when I click on Add Row it recreates a new grid instead of add extra rows to the bottom of the grid.
|

December 1st, 2007, 10:31 AM
|
Authorized User
|
|
Join Date: Jun 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hello, I updated my logic, but it still seems like I am just recreating a new table. Please tell me where you think I am going wrong this time around. Thank you in advbance:
Protected Sub AddRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddRow.Click
Dim dt As New partdb.tblReceivingDataTable()
dt.Columns.Add(New Data.DataColumn("LineNO"))
If (IsNumeric(RowCount.Text)) Then
Dim i As Integer
For i = 1 To CInt(RowCount.Text)
'Dim ds As DataSet = New DataSet(): Donot need using existing dataset partdb.tblReceivingDataTable
'Adding a row to the dataset datatable assigned to the grid.datasource
Dim dr As Data.DataRow
dr = dt.NewRow()
dr("CustomerID") = ""
dr("BA") = "False"
dr("Barcode") = ""
dr("RP") = "False"
dr("RD") = ""
dr("CL") = "False"
dr("LocationID") = ""
dr("Part_SerialNumber") = ""
dr("Part_number") = ""
dr("UPC") = ""
dr("unit_price") = "0"
dr("PropertyTypeID") = ""
dr("CC") = ""
dr("RC") = ""
dr("Received_Qty") = "0"
dr("UIID") = ""
dr("CCID") = ""
dr("Time_stamp_Received") = Date.Now
dr("Remarks") = ""
dr("PFlag") = "False"
dr("Archive") = "False"
'dt.Rows.Add("", "", "", "", "", "", "", "", "", "", 0, "", "", "", 0, "", "", "", "")
dt.Rows.Add(dr)
dt.AcceptChanges()
'Bind & Show New row(s) in grid
gvParts.DataSource = dt
gvParts.DataBind()
Next
End If
End Sub
|

December 4th, 2007, 09:41 AM
|
Authorized User
|
|
Join Date: Jun 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I added dt = (DataTable)gvParts.DataSource and received error: DataTable is a type and cannot be used as an expression. and end of statement expected. I have tried the statement different ways and no luck.
|

December 6th, 2007, 02:37 AM
|
Authorized User
|
|
Join Date: Jun 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Helllo,
I updated the my logic and added page load code to bind to a blank data table with all the columns I need to add a blank row when using the Add Row Click but now I receive the error: Column 'CustomerID' does not belong to table. Please tell me what I am doing wrong?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim DT As New DataTable
If (Not Page.IsPostBack) OrElse (Not (TypeOf Session("DT") Is DataTable)) Then
'Create Empty Datatable
'DT = CType(gvParts.DataSource, DataTable)
DT = New DataTable("AddExtraRow")
DT.Columns.Add("LN", Type.GetType("System.String"))
DT.Columns.Add("CustomerID", Type.GetType("System.String"))
DT.Columns.Add("BarcodeAttached", Type.GetType("System.String"))
DT.Columns.Add("Barcode", Type.GetType("System.String"))
DT.Columns.Add("RevisionPart", Type.GetType("System.String"))
DT.Columns.Add("RevisionDescription", Type.GetType("System.String"))
DT.Columns.Add("ComponentLoaction", Type.GetType("System.String"))
DT.Columns.Add("LocationID", Type.GetType("System.String"))
DT.Columns.Add("Part_SerialNumber", Type.GetType("System.String"))
DT.Columns.Add("Part_number", Type.GetType("System.String"))
DT.Columns.Add("UPC", Type.GetType("System.String"))
DT.Columns.Add("unit_price", Type.GetType("System.String"))
DT.Columns.Add("PropertyTypeID", Type.GetType("System.String"))
DT.Columns.Add("ComponentClassification", Type.GetType("System.String"))
DT.Columns.Add("RecvCondition", Type.GetType("System.String"))
DT.Columns.Add("Received_Qty", Type.GetType("System.String"))
DT.Columns.Add("UOI", Type.GetType("System.String"))
DT.Columns.Add("ConditionCodeID", Type.GetType("System.String"))
DT.Columns.Add("Time_stamp_Received", Type.GetType("System.String"))
DT.Columns.Add("Remarks", Type.GetType("System.String"))
'Store the table in the Session variable
Session("DT") = DT
' Bind the DataGrid to an empty table
'gvParts.DataSource = DT
' gvParts.DataBind()
Else
' Retrieve the table from the session variable
DT = CType(Session("DT"), DataTable)
End If
End Sub
Protected Sub AddRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddRow.Click
If (IsNumeric(RowCount.Text)) Then
Dim i As Integer
For i = 1 To CInt(RowCount.Text)
Dim DT As New DataTable
Dim DR As DataRow = DT.NewRow()
DR("CustomerID") = RowCount.Text
DT.Rows.Add(DR)
'Bind & Show New row(s) in grid
gvParts.DataSource = DT
gvParts.DataBind()
Next
End If
End Sub
|
|
 |