Home All Groups Group Topic Archive Search About

DataGrid SortCommand Event not fired on dynamically generated datagrid

Author
5 May 2005 10:03 PM
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!!!!!

Author
6 May 2005 12:13 PM
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.

Show quoteHide quote
"biru" <wiew***@gmail.com> wrote in message
news:1115330599.013848.243380@g14g2000cwa.googlegroups.com...
>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!!!!!
>
Author
6 May 2005 1:38 PM
biru
Okay.. but what if the columns generated depends on the search criteria
which is defined when the Search button is hit?