Gridview control and sorting a calculated templatefield column ?

M

markm75c

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!
 
P

PeterKellner

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
 
M

markm75c

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?
 

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

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top