On 26 May 2006 08:30:22 -0700,
ma******@msn.com 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