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!!!!!
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!!!!!