I have a gridview (Gridview1) that is populated via a sql statement. I iterate through the gridview and change column cell values based on calculations. I would like to sort the gridview by the column that has the calculated values (row.cells(6)). I was unsuccessful with that so I attempted to create a datatable from gridview1 that would populate gridview2. That is also not working for me. Is this the correct way to do this or do I need to try to accomplish this without the use of gridviews? My gridview databound event code is below:
Sub RowDataBoundEvent(ByVal o As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
Dim i As Integer = 1
Dim prevItem As String = ""
Dim prevRemQty As Integer
Dim Table As New DataTable
For Each row In GridView1.Rows
If display = "big" Then
row.font.size = 18
End If
If row.RowType = DataControlRowType.DataRow Then
If row.Cells(2).Text = prevItem Then
row.Cells(6).Text = prevRemQty + row.Cells(5).Text
End If
prevItem = row.Cells(2).Text
prevRemQty = row.Cells(6).Text
End If
Next
For Each row In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
If row.Cells(6).Text < 0 Then
row.Cells(6).Text = 0
End If
Dim hlFile = DirectCast(row.FindControl("HlFile"), HyperLink)
' set the hyperlink url just as you please
hlFile.NavigateUrl = ("ShippingInstr.aspx?order=" + row.cells(0).Text + "&line=" + row.cells(1).Text)
End If
Next
Try
Dim HeaderRow As TableRow = CType(GridView1.HeaderRow, TableRow)
For Each c As DataControlField In GridView1.Columns
Dim dc As New DataColumn
dc.ColumnName = c.HeaderText
error_label.Text = error_label.Text + c.HeaderText
Table.Columns.Add(dc)
Next
Table.Columns.Add()
Table.Rows.Add(HeaderRow)
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
Table.Rows.Add(CType(row, TableRow))
End If
Next
GridView2.DataSource = Table
GridView2.DataBind()
GridView2.Visible =True
Catch ex As Exception
error_label.Text = error_label.Text + Table.Columns.Count().ToString
error_label.Text = error_label.Text
End Try
End Sub
Sub RowDataBoundEvent(ByVal o As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
Dim i As Integer = 1
Dim prevItem As String = ""
Dim prevRemQty As Integer
Dim Table As New DataTable
For Each row In GridView1.Rows
If display = "big" Then
row.font.size = 18
End If
If row.RowType = DataControlRowType.DataRow Then
If row.Cells(2).Text = prevItem Then
row.Cells(6).Text = prevRemQty + row.Cells(5).Text
End If
prevItem = row.Cells(2).Text
prevRemQty = row.Cells(6).Text
End If
Next
For Each row In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
If row.Cells(6).Text < 0 Then
row.Cells(6).Text = 0
End If
Dim hlFile = DirectCast(row.FindControl("HlFile"), HyperLink)
' set the hyperlink url just as you please
hlFile.NavigateUrl = ("ShippingInstr.aspx?order=" + row.cells(0).Text + "&line=" + row.cells(1).Text)
End If
Next
Try
Dim HeaderRow As TableRow = CType(GridView1.HeaderRow, TableRow)
For Each c As DataControlField In GridView1.Columns
Dim dc As New DataColumn
dc.ColumnName = c.HeaderText
error_label.Text = error_label.Text + c.HeaderText
Table.Columns.Add(dc)
Next
Table.Columns.Add()
Table.Rows.Add(HeaderRow)
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
Table.Rows.Add(CType(row, TableRow))
End If
Next
GridView2.DataSource = Table
GridView2.DataBind()
GridView2.Visible =True
Catch ex As Exception
error_label.Text = error_label.Text + Table.Columns.Count().ToString
error_label.Text = error_label.Text
End Try
End Sub