Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to sort a GridView populated by DataView in ASP.NET 2.0 using C#
    primarykey
    data
    text
    <p>I am working on extending an ASP.NET 2.0 application using an InterBase database. My experience is in PHP/MySQL, so my familiarity with ASP is currently in the 2-week range, and is pieced together from examining a co-worker's code, the first 90 pages of my ASP book, and Google. In the application, I have an SqlDataSource control connecting to my database and selecting the information I need. Then the results are copied into a DataView where I modify the data in one of the columns, and then I push that DataView to a GridView for output. The issue I'm having is that I cannot sort the GridView at this point. I followed instructions here: <a href="http://forums.asp.net/p/956540/1177923.aspx" rel="nofollow">http://forums.asp.net/p/956540/1177923.aspx</a>, but to no avail.</p> <p>Here is the page code:</p> <pre><code> &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;asp:SqlDataSource ID="Products" runat="server" ConnectionString="&lt;%$ ConnectionStrings:ConnectionString1 %&gt;" ProviderName="&lt;%$ ConnectionStrings:ConnectionString1.ProviderName %&gt;" OnLoad="ProductsDS_Load" OnSelected="ProductsDS_Selected" DataSourceMode="DataSet"&gt; &lt;/asp:SqlDataSource&gt; &lt;br /&gt; &lt;asp:Label ID="testlabel" runat="server"&gt;&lt;/asp:Label&gt; &lt;br /&gt; &lt;asp:Label ID="testlabel2" runat="server"&gt;&lt;/asp:Label&gt; &lt;br /&gt;&lt;br /&gt; This table lists all 2000+ numbered projects which are at least partially in process.&lt;br /&gt; The Project number link leads to a more detailed view of that project.&lt;br /&gt; &lt;br /&gt; &lt;asp:Label runat="server" ID="numrows"&gt;&lt;/asp:Label&gt; results returned. &lt;br /&gt; &lt;asp:GridView ID="ProductsView" runat="server" EnableModelValidation="True" AutoGenerateColumns="False" CellPadding="4" OnSorting="ProductsView_Sorting" ForeColor="#333333" GridLines="None" AllowSorting="True"&gt; &lt;AlternatingRowStyle BackColor="White" /&gt; &lt;Columns&gt; &lt;asp:HyperLinkField HeaderText="Project" SortExpression="PROJECT" DataTextField="PROJECT" Target="subweeklyreport" DataNavigateUrlFields="PROJECT" DataNavigateUrlFormatString="Products.aspx?p={0}" /&gt; &lt;asp:BoundField Visible="false" DataField="PROJECTID" /&gt; &lt;asp:BoundField DataField="PART" HeaderText="Part #" SortExpression="PART" /&gt; &lt;asp:BoundField DataField="DESCRIPTION" HeaderText="Description" SortExpression="DESCRIPTION" /&gt; &lt;asp:BoundField DataField="ENGMGR" HeaderText="Eng. Mgr." SortExpression="ENGMGR" /&gt; &lt;/Columns&gt; &lt;EditRowStyle BackColor="#7C6F57" /&gt; &lt;FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /&gt; &lt;HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /&gt; &lt;PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /&gt; &lt;RowStyle BackColor="#E3EAEB" /&gt; &lt;SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /&gt; &lt;/asp:GridView&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p>And here is the code behind:</p> <pre><code>protected void ProductsDS_Load(object sender, EventArgs e) { string SQLQuery = Query would go here; testlabel2.Text = SQLQuery; Products.SelectCommand = SQLQuery; Products.DataBind(); DataView dv = (DataView)Products.Select(new DataSourceSelectArguments()); foreach (DataRow dr in dv.Table.Rows) { string name = dr["ENGMGR"].ToString(); string[] explode = name.Split(' '); string newname; if (explode.Length == 3) { newname = explode[2] + ", " + explode[0]; } else { newname = explode[1] + ", " + explode[0]; } dr["ENGMGR"] = newname; //testlabel.Text = dr["ENGMGR"].ToString(); } Products.DataBind(); //ProductsView.DataSourceID = "Products"; ProductsView.DataSource = dv; ProductsView.DataBind(); ProductsView.Enabled = true; ProductsView.Visible = true; } protected void ProductsDS_Selected(object sender, SqlDataSourceStatusEventArgs e) { numrows.Text = e.AffectedRows.ToString(); } protected void ProductsView_Sorting(object sender, GridViewSortEventArgs e) { DataTable dataTable = ProductsView.DataSource as DataTable; if (dataTable != null) { DataView dataView = new DataView(dataTable); dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection); ProductsView.DataSource = dataView; ProductsView.DataBind(); } } private string ConvertSortDirectionToSql(SortDirection sortDirection) { string newSortDirection = String.Empty; switch (sortDirection) { case SortDirection.Ascending: newSortDirection = "ASC"; break; case SortDirection.Descending: newSortDirection = "DESC"; break; } return newSortDirection; } </code></pre> <p>What I think is happening is that whenever the GridView does the postback for the sort, it causes the query to be executed again and overwrite the attempt to sort the existing data in the GridView, but I don't know enough about ASP right now to prevent this behavior. Any help would be much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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