On 26 May 2006 08:30:22 -0700,
ma******@msn.co m 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="Defau lt2.aspx.cs" Inherits="Defau lt2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView AutoGenerateCol umns="False" ID="GridView1"
runat="server" DataSourceID="S qlDataSource1" AllowSorting="T rue">
<Columns>
<asp:BoundFie ld DataField="Coun try"
HeaderText="Cou ntry" SortExpression= "Country" />
<asp:TemplateFi eld HeaderText="Col umn1"
SortExpression= "Column1">
<EditItemTempla te>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("Column1") %>'></asp:TextBox>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("Column1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Dou ble count"
SortExpression= "Column1">
<ItemTemplate >
<asp:Label ID="Label1" runat="server"
Text='<%# (string) GetComputedFiel d( (int)
Eval("Column1") ) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
</Columns>
</asp:GridView>
<asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
ConnectionStrin g="<%$ ConnectionStrin gs:NorthwindCon nectionString %>"
SelectCommand=" SELECT
dbo.Employees.C ountry,count(co untry)
FROM
dbo.Employees
GROUP BY
dbo.Employees.C ountry">
</asp:SqlDataSour ce>
</div>
</form>
</body>
</html>
using System;
using System.Web.UI;
public partial class Default2 : Page
{
protected void Page_Load(objec t sender, EventArgs e)
{
}
protected string GetComputedFiel d(int i)
{
int j = i*2;
return j.ToString();
}
}
Peter Kellner
http://peterkellner.net