Manipulate data in gridview and populate new gridview

bsmithsd

New member
Joined
Jan 31, 2013
Messages
3
Programming Experience
5-10
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
If Trim(row.Cells(6).Text) <= 0 And Not row.Cells(8).Text.Equals(" ") And ((Trim(row.Cells(9).Text) <> Trim(row.Cells(10).Text) And row.cells(13).Text = "Y") Or (Trim(row.Cells(9).Text) = Trim(row.Cells(10).Text))) Then
row.BackColor = Drawing.Color.LightGreen
row.Font.Bold = True
End If
If Trim(row.Cells(3).Text) < Today Then
row.Cells(2).ForeColor = Drawing.Color.Red
End If
If Trim(row.Cells(9).Text) <> Trim(row.Cells(10).Text) And row.cells(13).Text <> "Y" Then
row.Cells(9).ForeColor = Drawing.Color.Red
row.Cells(10).ForeColor = Drawing.Color.Red
End If
If Trim(row.Cells(11).Text) <> Trim(row.Cells(10).Text) Then
row.Cells(11).ForeColor = Drawing.Color.Red
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
error_label.Text = "Here"
If row.RowType = DataControlRowType.DataRow Then
Table.Rows.Add(CType(row, TableRow))
error_label.Text = error_label.Text + "jere"
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
 
Last edited:
Back
Top