0

I have a Gridview that I need to add a button for each row and need to do this programmatically. I am able to add the button, but I am missing something as it is creating a new column and adding the button only in the new column. When the button is added, I will need to have it go go to a url for that particular product.
Part of the problem that is confusing me is that I already have an image that I am adding.
The Gridview is created programmatically to start with.
Here is the code I have so far:

Private Function PivotTable(origTable As DataTable) As DataTable
        Dim newTable As New DataTable()
        Dim dr As DataRow = Nothing

        'Add Columns to new Table
        For i As Integer = 0 To origTable.Rows.Count
            newTable.Columns.Add(New DataColumn(origTable.Columns(i).ColumnName, GetType([String])))
        Next

        'Execute the Pivot Method
        For cols As Integer = 0 To origTable.Columns.Count - 1
            dr = newTable.NewRow()
            For rows As Integer = 0 To origTable.Rows.Count - 1
                If rows < origTable.Columns.Count Then
                    dr(0) = origTable.Columns(cols).ColumnName
                    ' Add the Column Name in the first Column
                    dr(rows + 1) = origTable.Rows(rows)(cols)
                End If
            Next
            'add the DataRow to the new Table rows collection
            newTable.Rows.Add(dr)
        Next
        Return newTable
    End Function
    Private Sub BindGridView()

        Dim connString As String = ConfigurationManager.ConnectionStrings("ConnectionString1").ConnectionString
        Dim connection As New OleDbConnection(connString)
        Dim dt As New DataTable
        Try
            connection.Open()
            Dim sqlStatement As String = "SELECT * FROM WEB"
            Dim sqlCmd As New OleDbCommand(sqlStatement, connection)
            Dim sqlDa As New OleDbDataAdapter(sqlCmd)
            sqlDa.Fill(dt)
            If dt.Rows.Count > 0 Then
                'Bind the First GridView with the original data from the DataTable
                'GridView1.DataSource = dt
                'GridView1.DataBind()

                'Pivot the Original data from the DataTable by calling the
                'method PivotTable and pass the dt as the parameter
                Dim pivotedTable As DataTable = PivotTable(dt)
                GridView2.DataSource = pivotedTable

                GridView2.DataBind()
            End If
        Catch ex As System.Data.SqlClient.SqlException
            Dim msg As String = "Fetch Error:"
            msg += ex.Message
            Throw New Exception(msg)
        Finally
            connection.Close()
        End Try
    End Sub
    Protected Sub GridView2_RowDataBound(sender As Object, e As GridViewRowEventArgs)
        If e.Row.RowIndex = 0 Then
            For Each cell As TableCell In e.Row.Cells
                Dim imagePath As [String] = cell.Text
                Dim image As New Image()
                image.ImageUrl = imagePath
                cell.Text = ""
                cell.Controls.Add(image)
            Next
        End If

    End Sub

It is the row below the Images that I need to add a button.
Any help will be greatly appreciated

2
Contributors
2
Replies
11
Views
4 Years
Discussion Span
Last Post by clausont
0

Thanks geniusvishal! I have the buttons added now, but still have a couple of problems:
One problem is there is one column (first column) where I don't want the button - it should remain blank and I am not sure how to do this. The second is the button event is not firing. I see one of the links is helping with the button event, but I am not getting it to work yet.
Any thoughts?
Here is the code as I have it now:

 Protected Sub GridView2_RowDataBound(sender As Object, e As GridViewRowEventArgs)
        If e.Row.RowIndex = 0 Then
            For Each cell As TableCell In e.Row.Cells
                Dim imagePath As [String] = cell.Text

                Dim image As New Image()
                image.ImageUrl = imagePath

                cell.Text = ""
                cell.Controls.Add(image)
            Next
        End If
        If e.Row.RowIndex = 1 Then
            For Each cell As TableCell In e.Row.Cells
                Dim lb As New Button()
                lb.Text = "Apply"
                lb.CommandName = "applicationlink"
                AddHandler lb.Command, AddressOf button_Command
                cell.Controls.Add(lb)
            Next
        End If
    End Sub
    Protected Sub button_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
        If e.CommandName = "applicationlink" Then
            'This is to test  
            Dim lb As Button = DirectCast(sender, Button)
            'lb.Text = "OK"
            Response.Redirect("http://www.ehealthlink.com")
        End If
    End Sub
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.