Note that there are some explanatory texts on larger screens.

plurals
  1. POGridView SORTING of DYNAMIC Columns and Data Source Scope issues
    text
    copied!<p><strong>READ FIRST</strong> I re-wrote this to make it more readable. If you want to skip right to the chase, look at the ALL CAPS comments in the code blocks. All necessary code has been included for debugging.</p> <p>I've searched multiple forums (including ASP.NET), and the MSDN library and cannot fix this >.&lt;</p> <p><strong>GOAL</strong>: To dynamically generate a table/grid-like "status report" containing up to 20+ depending on user-specified columns (<em>consideration: should be able to be stored in a cookie for user prefs</em>). This grid will contain data provided from a View on the SQL Server, and row must be clickable. Pagination was super easy to implement, but the <strong>sorting</strong> has proven to be a nasty challenge.</p> <p><strong>ISSUE (Scope)</strong>: I decided to re-post all the relevant code to make it easier to troubleshoot.</p> <pre><code>Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then chooseColumns() End If End Sub Protected Function queryToDataSet() As ArrayList 'Code removed: Function returns the Columns to select from End Function 'Handles the button click to build the `GridView` from the selected columns Protected Sub ShowGrid(ByVal sender As Object, ByVal e As EventArgs) Handles btnSub.Click For Each item As ListItem In chkFields.Items If item.Selected Then Dim b As New BoundField() b.DataField = item.Value b.HeaderText = item.Value b.SortExpression = item.Value statusReportGrid.Columns.Add(b) End If Next --&gt; Me.GetData() 'HERE'S THE SCOPING ISSUE... UPON EXIT THE DATASOURCE IS LOST End Sub Private Sub GetData() statusReportGrid.DataSource = StatusDS statusReportGrid.DataBind() End Sub Protected Sub statusReportGrid_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Handles statusReportGrid.Sorting 'CODE BREAKS HERE because the DataSource is no longer in scope Dim dataTable As DataTable = TryCast(statusReportGrid.DataSource, DataTable) If dataTable IsNot Nothing Then Dim dvSortedView As New DataView(dataTable) 'The following line (when working properly) should return a string ' something like "StatusColumn DESC" for example. This format ' doesn't make sense to me and doesn't seem correct. dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection) statusReportGrid.DataSource = dvSortedView statusReportGrid.DataBind() End If End Sub Private Function getSortDirectionString(ByVal sortDirection As SortDirection) As String Dim newSortDirection As String = String.Empty Select Case sortDirection Case sortDirection.Ascending newSortDirection = "ASC" Case sortDirection.Descending newSortDirection = "DESC" End Select Return newSortDirection End Function </code></pre> <p><strong>ASP CONTROLS</strong> <code>GridView</code> control (minus the styling stuff) - The tutorials I've read said to set the <code>EnableViewState</code> to false. When trying it with a setting of true I get repeated columns on refresh.</p> <pre><code>&lt;asp:GridView ID="statusReportGrid" runat="server" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" EnableViewState="False" ShowHeaderWhenEmpty="True"&gt;&lt;/asp:GridView&gt; </code></pre> <p>Primary <code>SqlDataSource</code> control for the status report content.</p> <pre><code>&lt;asp:SqlDataSource ID="StatusDS" runat="server" ConnectionString="&lt;%$ ConnectionStrings:StatusDBConnectionString %&gt;" SelectCommand="SELECT * FROM [StatusTable]"&gt; &lt;/asp:SqlDataSource&gt; </code></pre> <p><strong>THE PROBLEM</strong> I just discovered this to be an issue of scope as once my calling function that causes the <code>DataSource</code> to be bound to my <code>GridView</code> exits, the <code>DataSource</code> then becomes empty. I would certainly appreciate some help resolving my scoping issues so that my <code>DataSource</code> remains available and modifiable by all functions in the codebehind. It should be persistent as long as the page is being displayed more or less.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload