System.IndexOutOfRange Exception Sending Gridview Values to DataTable
- by SidC
Hello,
I am writing an ASP.NET 3.5 application and need to send gridview values to a datatable for use in a listbox control as part of a quote process. I have written the following VB code in my Page_Load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dtSelParts As DataTable = New DataTable("dtSelParts")
Dim column As DataColumn = New DataColumn
column.ColumnName = "PartName"
column.ColumnName = "PartNumber"
column.ColumnName = "Quantity"
dtSelParts.Columns.Add(column)
For Each row As GridViewRow In MySearch.Rows
Dim drSelParts As DataRow
drSelParts = dtSelParts.NewRow()
For i As Integer = 0 To row.Cells.Count - 1
drSelParts(i) = row.Cells(i).Text
Next
Next
End Sub
When I run the partsearch.aspx page, I enter values in the row textbox for parts I want included in the listbox (to be included in quote). However, I receive the error message System.Index.OutOfRangeException: Cannot find column 1 which occurs on the line drSelParts(i) = row.Cells(i).Text.
How might I correct the code and resolve the error?
Thanks,
Sid