DataGrid SortCommand Event not fired on dynamically generated datagrid

B

biru

I have a page that allows the user to do a search and the page would
basically display the search result at the bottom of the page when the
user hits "Search". The result is shown in a datagrid that has columns
generated dynamically using ItemTemplate and ITemplate implementing
classes. Some of the columns added programatically are also
BoundColumns with SortExpression specified.

The code goes as follows:
(it's in a sub that's called when "Search" button is hit)
Dim idCol As BoundColumn = New BoundColumn
idCol.Visible = False
idCol.DataField = "DocumentID"
dbgTest.Columns.Add(idCol)

Dim iconTemplate As TemplateColumn = New TemplateColumn
iconTemplate.ItemStyle.Width = New Unit(2, UnitType.Percentage)
iconTemplate.ItemTemplate = New FileIconTemplate
dbgTest.Columns.Add(iconTemplate)

If bViewerAvail Then
Dim chkSelectTemplate As TemplateColumn = New TemplateColumn
chkSelectTemplate.ItemStyle.Width = New Unit(2,
UnitType.Percentage)
chkSelectTemplate.ItemTemplate = New FileCheckboxTemplate
dbgTest.Columns.Add(chkSelectTemplate)
End If

Dim docLinkTemplate As TemplateColumn = New TemplateColumn
docLinkTemplate.ItemTemplate = New FilenameTemplate
docLinkTemplate.HeaderTemplate = New FilenameHeaderTemplate
dgResult.Columns.Add(docLinkTemplate)

AddBoundColumnToDataGrid(dbgTest, "File Type", "Extension")
AddBoundColumnToDataGrid(dbgTest, "Size", "Size")
AddBoundColumnToDataGrid(dbgTest, "Description", "Description")

Dim opTemplate As TemplateColumn = New TemplateColumn
opTemplate.HeaderStyle.Width = New Unit(5, UnitType.Percentage)
opTemplate.ItemStyle.HorizontalAlign = HorizontalAlign.Center
opTemplate.ItemTemplate = New
OperationsTemplate(Convert.ToBoolean(Session("IsAdministrator").ToString))
opTemplate.HeaderText = "Operations"
dbgTest.Columns.Add(opTemplate)

dbgTest.CurrentPageIndex = 0
dbgTest.DataSource = dstResults
Session("dstResults") = dstResults
AddHandler dbgTest.SortCommand, AddressOf dbgResults_SortCommand
dbgTest.DataBind()

Private Sub AddBoundColumnToDataGrid(ByRef dgResult As DataGrid, ByVal
psHeaderText As String, ByVal psDataField As String)
Dim bc As BoundColumn = New BoundColumn
bc.HeaderText = psHeaderText
bc.SortExpression = psDataField
bc.DataField = psDataField
dgResult.Columns.Add(bc)
End Sub


Public Sub dbgTest_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs)
If SortColumn = e.SortExpression Then
SortAscend = Not SortAscend
Else
SortAscend = True
End If
SortColumn = e.SortExpression
End Sub
End Class

I know my code to enable the sorting is not complete yet.
However, when I click on the header of a column in the datagrid, the
SortCommand method was not even executed.

Is there a way to make Sorting works with dynamically generated
datagrid?

Thanks!!!!!
 
C

Cole Trickle

You have to make sure the columns are defined in when the page is
initialised.
I create a InitDataGrids function that runs in Page_Init. Then when the user
hits the search button do your databinding then.
 
B

biru

Okay.. but what if the columns generated depends on the search criteria
which is defined when the Search button is hit?
 

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

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top