DataGrid SortCommand Event not fired on dynamically generated datagrid

Discussion in 'ASP .Net Web Controls' started by biru, May 5, 2005.

  1. biru

    biru Guest

    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!!!!!
     
    biru, May 5, 2005
    #1
    1. Advertising

  2. biru

    Cole Trickle Guest

    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.

    "biru" <> wrote in message
    news:...
    >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!!!!!
    >
     
    Cole Trickle, May 6, 2005
    #2
    1. Advertising

  3. biru

    biru Guest

    Okay.. but what if the columns generated depends on the search criteria
    which is defined when the Search button is hit?
     
    biru, May 6, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Sturgeon

    DataGrid SortCommand event not triggered

    John Sturgeon, Nov 26, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    582
    John Sturgeon
    Dec 2, 2003
  2. Mo
    Replies:
    0
    Views:
    464
  3. =?Utf-8?B?UGF1bA==?=

    SortExpression makes SortCommand event not to trig

    =?Utf-8?B?UGF1bA==?=, Sep 22, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    471
    =?Utf-8?B?UGF1bA==?=
    Sep 22, 2005
  4. Jules Dekker

    Dynamically created TemplateColumn does not raise SortCommand

    Jules Dekker, Feb 13, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    139
    Jules Dekker
    Feb 13, 2004
  5. Manav

    SortCommand Event in DataGrid

    Manav, Feb 25, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    131
    Scott Mitchell [MVP]
    Feb 25, 2004
Loading...

Share This Page