Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Row into Gridview without modifiying datasource

Status
Not open for further replies.

CassidyHunt

IS-IT--Management
Jan 7, 2004
688
US
Does anyone have a method of inserting a row into a gridview perhaps on the prerender or render events of the gridview control?

Thanks

Cassidy
 
How about just adding the row to the DataTable (or whatever you are using as the source) before binding?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Sorry, just noticed the thread title! Any particular reason you don't want to use this method?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I actually just found the answer to this. The reason I do not want add the row to the datasource is because I need to build a server control that will handle group by and summary methods.

The solution that I found is this:

Code:
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        If _SortedFlag Then

            Dim table As Table = DirectCast(grid1.Controls(0), Table)

            Dim htLookUp As New Hashtable

            For Each row As GridViewRow In grid1.Rows

                Dim realIndex As Integer = table.Rows.GetRowIndex(row)

                Dim text As String = row.Cells(_SortColumnIndex).Text

                If Not htLookUp.ContainsKey(text) Then
                    htLookUp.Add(text, Nothing)

                    Dim newHeaderRow As New GridViewRow(realIndex, realIndex, DataControlRowType.DataRow, DataControlRowState.Normal)
                    Dim newCell As New TableCell()
                    newHeaderRow.Cells.Add(newCell)
                    newCell.ColumnSpan = grid1.Columns.Count
                    newCell.BackColor = Drawing.Color.Gray
                    newCell.ForeColor = Drawing.Color.White
                    newCell.Font.Bold = True
                    newCell.Text = _SortColumnHeader & " : " & text

                    table.Controls.AddAt(realIndex, newHeaderRow)

                End If
            Next

        End If
        MyBase.Render(writer)
    End Sub

Actually I producing my index's differently since I need to place multiple sort columns.

I don't know if anyone would be interested by I will post my complete extended gridview class here.

Thanks

Cassidy
 
I don't know if anyone would be interested by I will post my complete extended gridview class here.
Go for it...I'm sure it will help someone.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top