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