Adding ImageButton To DataGrid Header

D

Dagger

Hello

This is probable a simple one for all you experts out there. I was wondering what the most efficient way of dynamically assigning a URL String to an ImageButton and then adding that Image button to a DataGrid Heade

Thank you in advance for your hel

RD
 
K

Ken Cox [Microsoft MVP]

Hi RD,

Not sure about efficiency, but here's a way to do it in the codebehind. Just
create an imagebutton, add it to a cell, add that to a new header, wipe the
old header and bake for .01 seconds....

Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, _
ByVal e As _
System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Header Then
' Get the collection of cells from the grid
Dim tcells As TableCellCollection
' Create an imagebutton
Dim imgBtn As New ImageButton
' Assign the URL
imgBtn.ImageUrl = "http://www.gc.ca/images/flag.gif"
' Get the collection of existing cells so we can get a count
tcells = e.Item.Cells
' Create a new cell
Dim fcell As New TableCell
' Add the image button to the new table cell
fcell.Controls.Add(imgBtn)
' Span the cell to however many columns there are
fcell.ColumnSpan = tcells.Count
' Create a new header object
Dim dgItemHeader As New DataGridItem _
(0, 0, ListItemType.Header)
Dim dgItemOldHeader As DataGridItem
'Create an instance of the header
'from the current header object
dgItemOldHeader = e.Item
' Delete the existing header
DataGrid1.Controls(0).Controls.Remove(dgItemOldHeader)
' Add the cell to the header
dgItemHeader.Cells.Add(fcell)
dgItemHeader.Visible = True
' Add the header to the datagrid
DataGrid1.Controls(0).Controls.Add(dgItemHeader)
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top