By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,657 Members | 774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,657 IT Pros & Developers. It's quick & easy.

Gridview control and sorting a calculated templatefield column ?

P: n/a
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 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
May 27 '06 #2

P: n/a
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, 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


Jun 14 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.