Gridview control and sorting a calculated templatefield column ?

Discussion in 'ASP .Net' started by markm75c@msn.com, May 26, 2006.

  1. Guest

    Does anyone know of a way to sort a column which isnt databound or an
    actual field in the database, but is derived from a method?

    IE. I have a grid showing stats for softball, with a calculated field
    called AVG, I want to be able to sort based on AVG when they click on
    AVG.

    I can't seem to find a way to do this out there.

    Thanks!
     
    , May 26, 2006
    #1
    1. Advertising

  2. PeterKellner Guest

    On 26 May 2006 08:30:22 -0700, wrote:

    >Does anyone know of a way to sort a column which isnt databound or an
    >actual field in the database, but is derived from a method?
    >
    >IE. I have a grid showing stats for softball, with a calculated field
    >called AVG, I want to be able to sort based on AVG when they click on
    >AVG.
    >
    >I can't seem to find a way to do this out there.
    >
    >Thanks!


    I'm having trouble getting it not to sort a column. Here is a simple
    example I through together.

    <%@ Page Language="C#" AutoEventWireup="true"
    CodeFile="Default2.aspx.cs" Inherits="Default2" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>

    <asp:GridView AutoGenerateColumns="False" ID="GridView1"
    runat="server" DataSourceID="SqlDataSource1" AllowSorting="True">
    <Columns>
    <asp:BoundField DataField="Country"
    HeaderText="Country" SortExpression="Country" />
    <asp:TemplateField HeaderText="Column1"
    SortExpression="Column1">
    <EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server"
    Text='<%# Bind("Column1") %>'></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server"
    Text='<%# Bind("Column1") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Double count"
    SortExpression="Column1">

    <ItemTemplate>
    <asp:Label ID="Label1" runat="server"
    Text='<%# (string) GetComputedField( (int)
    Eval("Column1") ) %>'></asp:Label>
    </ItemTemplate>

    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    SelectCommand="SELECT

    dbo.Employees.Country,count(country)
    FROM

    dbo.Employees
    GROUP BY
    dbo.Employees.Country">
    </asp:SqlDataSource>
    </div>
    </form>
    </body>
    </html>



    using System;
    using System.Web.UI;

    public partial class Default2 : Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected string GetComputedField(int i)
    {
    int j = i*2;
    return j.ToString();
    }
    }
    Peter Kellner
    http://peterkellner.net
     
    PeterKellner, May 27, 2006
    #2
    1. Advertising

  3. Guest

    Your example isnt the same as what I am doing in reality.

    Your sort Expression for the calculated field is set to "Column1",
    which is not a calculated field, its an actual database field.

    So like in my case, where I have a calculated field with expression
    like <%# GetAVG() %>, I would want to sort based on this function
    result (the average). I could say sortexpression="AB" for instance,
    but that isnt what I am trying to sort, I am trying to sort the
    calculated column called AVG, which is the biproduct of a custom
    function.

    Any thoughts?




    PeterKellner wrote:
    > On 26 May 2006 08:30:22 -0700, wrote:
    >
    > >Does anyone know of a way to sort a column which isnt databound or an
    > >actual field in the database, but is derived from a method?
    > >
    > >IE. I have a grid showing stats for softball, with a calculated field
    > >called AVG, I want to be able to sort based on AVG when they click on
    > >AVG.
    > >
    > >I can't seem to find a way to do this out there.
    > >
    > >Thanks!

    >
    > I'm having trouble getting it not to sort a column. Here is a simple
    > example I through together.
    >
    > <%@ Page Language="C#" AutoEventWireup="true"
    > CodeFile="Default2.aspx.cs" Inherits="Default2" %>
    >
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >
    > <html xmlns="http://www.w3.org/1999/xhtml" >
    > <head runat="server">
    > <title>Untitled Page</title>
    > </head>
    > <body>
    > <form id="form1" runat="server">
    > <div>
    >
    > <asp:GridView AutoGenerateColumns="False" ID="GridView1"
    > runat="server" DataSourceID="SqlDataSource1" AllowSorting="True">
    > <Columns>
    > <asp:BoundField DataField="Country"
    > HeaderText="Country" SortExpression="Country" />
    > <asp:TemplateField HeaderText="Column1"
    > SortExpression="Column1">
    > <EditItemTemplate>
    > <asp:TextBox ID="TextBox1" runat="server"
    > Text='<%# Bind("Column1") %>'></asp:TextBox>
    > </EditItemTemplate>
    > <ItemTemplate>
    > <asp:Label ID="Label1" runat="server"
    > Text='<%# Bind("Column1") %>'></asp:Label>
    > </ItemTemplate>
    > </asp:TemplateField>
    > <asp:TemplateField HeaderText="Double count"
    > SortExpression="Column1">
    >
    > <ItemTemplate>
    > <asp:Label ID="Label1" runat="server"
    > Text='<%# (string) GetComputedField( (int)
    > Eval("Column1") ) %>'></asp:Label>
    > </ItemTemplate>
    >
    > </asp:TemplateField>
    > </Columns>
    > </asp:GridView>
    > <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    > ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    > SelectCommand="SELECT

    > dbo.Employees.Country,count(country)
    FROM

    > dbo.Employees
    GROUP BY
    dbo.Employees.Country">
    > </asp:SqlDataSource>
    > </div>
    > </form>
    > </body>
    > </html>
    >
    >
    >
    > using System;
    > using System.Web.UI;
    >
    > public partial class Default2 : Page
    > {
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > }
    >
    > protected string GetComputedField(int i)
    > {
    > int j = i*2;
    > return j.ToString();
    > }
    > }
    > Peter Kellner
    > http://peterkellner.net
     
    , Jun 14, 2006
    #3
    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. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    calculated column in datagrid question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Mar 23, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    605
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Mar 23, 2005
  2. sloan
    Replies:
    2
    Views:
    4,921
    aryan_subhash
    Oct 17, 2007
  3. mesut
    Replies:
    0
    Views:
    583
    mesut
    Jun 29, 2007
  4. Rafia Tapia
    Replies:
    1
    Views:
    474
    Masudur
    Jul 29, 2007
  5. Valli
    Replies:
    3
    Views:
    2,963
    =?Utf-8?B?QW5kcmV3IEhheWVz?=
    Sep 28, 2007
Loading...

Share This Page