Dynamic Columns Sorting issue in DataGrid

J

Jordo

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
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top