Dynamic Columns Sorting issue in DataGrid

Discussion in 'ASP .Net' started by Jordo, Oct 31, 2003.

  1. Jordo

    Jordo Guest

    Help! I have a datagrid that is being populated after a button click
    to fill based on parameters set by the user at run-time. I am
    creating the columns programmatically from a datatable and setting the
    sort expression there. I have AllowSort set to True but when the grid
    shows up, the column headers don't have the hyperlink to allow
    postback and sort. Any ideas? Code snippet posted below:

    -------------------
    Private Sub RunReport(Optional ByVal SortCmd As String = "")
    Dim sqlStart As String, sql As String, sqlEnd As String

    sql = "select Query1, Query2 from AgedReceivablesQueries where
    AgeType='" & ddlAgeBy.SelectedValue & _
    "' and AgeRange='" & ddlAgeOptions.SelectedValue & "'
    and ReportType='" & ddlOptions.SelectedValue & "'"

    Dim con As New SqlConnection(scon)
    Dim cmd As New SqlCommand(sql, con)
    Dim dr As SqlDataReader

    con.Open()

    dr = cmd.ExecuteReader
    While dr.Read
    sqlStart = dr(0)
    sqlEnd = dr(1)
    End While
    dr.Close()
    con.Close()

    sql = sqlStart + CreateFilter() + Chr(13) + Chr(10) + sqlEnd

    cmd.CommandText = sql

    Dim dt As New DataTable("Aging")
    Dim da As New SqlDataAdapter(cmd)

    da.Fill(dt)
    dt.DefaultView.Sort = SortCmd

    Dim dtCol As DataColumn, i As Int16 = 0
    For Each dtCol In dt.Columns

    Dim dgCol As New BoundColumn
    dgCol.DataField = dtCol.ColumnName
    dgCol.HeaderText = dtCol.ColumnName
    dgCol.SortExpression = dtCol.ColumnName

    Select Case dtCol.DataType.Name
    Case "Decimal"
    dgCol.DataFormatString = "{0:#,###0}"
    Case "DateTime"
    dgCol.DataFormatString = "{0:d}"
    End Select

    dgAgingSummary.Columns.Add(dgCol)
    dgExcelGrid.Columns.Add(dgCol)
    i += 1
    Next

    Select Case ddlOptions.SelectedValue
    Case "D" 'detail
    dgAgingSummary.Columns(0).Visible = False
    End Select

    Select Case ddlOutput.SelectedValue
    Case "HTML"
    dgAgingSummary.AllowSorting = True
    dgAgingSummary.DataSource = dt
    dgAgingSummary.DataBind()

    Case "Excel"
    dgExcelGrid.DataSource = dt
    dgExcelGrid.DataBind()
    ' Set the content type to Excel.
    Response.ContentType = "application/vnd.ms-excel"
    ' Remove the charset from the Content-Type header.
    Response.Charset = ""
    ' Turn off the view state.
    Me.EnableViewState = False

    Dim tw As New System.IO.StringWriter
    Dim hw As New System.Web.UI.HtmlTextWriter(tw)

    ' Get the HTML for the control.
    dgExcelGrid.RenderControl(hw)
    ' Write the HTML back to the browser.
    Response.Write(tw.ToString())
    ' End the response.
    Response.End()

    End Select

    End Sub
     
    Jordo, Oct 31, 2003
    #1
    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. avnrao
    Replies:
    3
    Views:
    3,699
    Elad Volpin
    Jun 6, 2004
  2. =?Utf-8?B?amF2YXRvcGlh?=

    Dynamic columns not sorting in DataGrid

    =?Utf-8?B?amF2YXRvcGlh?=, Oct 2, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    517
    =?Utf-8?B?amF2YXRvcGlh?=
    Oct 2, 2004
  3. Replies:
    5
    Views:
    621
  4. Kostia

    Dynamic columns in DataGrid, Paging/Sorting and Data Caching

    Kostia, Apr 16, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    182
    Kostia
    Apr 16, 2004
  5. javatopia

    Dynamic columns and sorting/paging in DataGrid

    javatopia, Oct 19, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    133
    javatopia
    Oct 19, 2004
Loading...

Share This Page