Question 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

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
 
Just a tip on your post. I don't mean to be a smartass, but it's something I was getting wrong until the Admins straightened me out! When posting code snippets you should use the advanced editor, click on the little icon "VB" and when prompted for the "option" type "vb" in lower case. Also just paste your code as text, all the HTML color and whetever tags are completely unnecessary. Here is what it should look like with the correct XCODE tags applied, I made a few minor corrections by separating some words your editor had concatenated together, and adding a couple of "Exit Sub" statements also, plus you had not initialized a number of variables as "New", this may cause run-time problems otherwise.

Sub RowDataBoundEvent(ByVal o AsObject, ByVal e As System.EventArgs) Handles GridView1.DataBound

    Dim i As Integer = 1
    Dim prevItem AsString = ""
    Dim prevRemQty AsInteger
    Dim Table As New DataTable


Try
    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
        EndIf
    Next row

    For Each row In GridView1.Rows
        If row.RowType = DataControlRowType.DataRow Then
            If row.Cells(6).Text < 0 Then
                row.Cells(6).Text = 0
            EndIf
            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 row


        Dim HeaderRow As New TableRow = CType(GridView1.HeaderRow, TableRow)

        For Each c As New 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 c

        Table.Columns.Add()
        Table.Rows.Add(HeaderRow)

        For Each row 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
        Exit Sub

    Catch ex As Exception
        error_label.Text = error_label.Text + Table.Columns.Count().ToString
        error_label.Text = error_label.Text
        Exit Sub
    End Try
End Sub
 
Last edited:
Moderation note: Rob Sherratt, this is an ASP.Net request in Web Forms forum, GridView is an ASP.Net server control. I deleted two of your posts as they were not relevant for this platform.
 
Back
Top