473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Dynamic Columns Sorting issue in DataGrid

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
Nov 17 '05 #1
0 1035

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: JP011 | last post by:
Hello I have hit a major road block when it comes to building my dynamic datagrid. To make a long story short I need a dynamic datagrid because my connection string could change and I need the...
0
by: Karlo Swart via .NET 247 | last post by:
Hi. I am creating a dynamic datagrid. The columns are based on a request.querystring value. Eg: I will pass in a 'func=toys' parameter, which will cause the datagrid to load create columns...
6
by: Mark | last post by:
I have been working for quite some time on this issue which in theory should be quite simple. The problem is that the Cancel and Save events are not fired when their respective buttons are...
0
by: javatopia | last post by:
I am creating a data grid's columns on the fly in my web page. The Bind operation works just fine, and data is displayed correctly when I change the layout of the grid (add/remove columns). I...
1
by: Chad Devine | last post by:
Alright, I am posting my solution to a problem I've been having. I have finally found the answer, and it took me a while so I thought I'd provide the final solution here, since it may be benificial...
1
by: Patrick | last post by:
I need to build a multi-lingual application with asp.net. I do not want to have a version of the application per language used. To achieve this, I have recorded in my database all the labels and...
3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
0
by: gokulrajad | last post by:
Hi there, Iam trying to create a datagrid dynamically in asp .net (C#). It contains a leftmost column with the check box and few data columns and the last column is a hyperlink column. Please find...
1
by: ECD | last post by:
Hello all, I can usually find solutions to my .NET problems by searching these groups, but I'm stumped on this one. I have a datagrid in VB.NET (2.0 framework). I want to disable sorting on...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.