Getting GridViewRow data into DataRow in Visual Basic
Below is a loop I am trying to use to get the values from a GridViewRow into a DataRow object (using the godforsaken language of Visual Basic). However, on this line:
dr(i) = r.Cells(i).Text
I keep getting the following error message:
the value of type string cannot be converted to system.data.datarow
Can someone point me in the right direction on how to do this?
Dim rows As New List(Of GridViewRow)() For Each item As GridViewRow In grdExpProd.Rows rows.Add(item) Next Dim value As Integer = rows.Count Dim dt As New DataTable() For index As Integer = value - 1 To 0 Step -1 Dim dr As DataRow() Dim r As GridViewRow = rows(index) For i As Integer = 0 To r.Cells.Count - 1 dr(i) = r.Cells(i).Text Next dt.Rows.Add(dr) Next
I believe you need to be adding your text to the DataRows Item Property which will allow you access to the individual cells of the DataRow. You are also going to need to add the columns that exist in your GridRowView to your new DataTable.
For i = 1 To rows(0).Cells.Count dt.Columns.Add("Header" & i) Next For index As Integer = value - 1 To 0 Step -1 Dim dr As DataRow = dt.NewRow() Dim r As GridViewRow = rows(index) For i As Integer = 0 To r.Cells.Count dr.Item(i) = r.Cells(i).Text Next dt.Rows.Add(dr) Next