473,322 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Gridview control and sorting a calculated templatefield column ?

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
2 7963
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: washoetech | last post by:
I have a gridview control. In this grid view there is a column for the price of an item. Some of the prices have a dollar sign in front of it and some dont. How do I get rid of the dollar sign...
6
by: Nalaka | last post by:
Hi, I have a gridView (grid1), which as a templateColumn. In the template column, I have put in a gridView (grid2) and a ObjectDataSource (objectDataSource2). Question is... How to I pass the...
1
by: Miguel Dias Moura | last post by:
Hello, I have a GridView in my page which is created in runtime. It works fine. My page has 2 Asp Buttons: - The HIDE button makes GridView.Visible = False; - The SHOW button makes...
1
by: Giovanni | last post by:
Dear Friends/Gurus, I have exhausted myself and have yet no solution to the following: I have an ASP.NET 2.0 Survey type application. On a page, I have placed a GridView which is bound to an...
3
by: sloan | last post by:
<asp:BoundField DataField="MyDate" DataFormatString="{0:d}" HeaderText="My Date" /> I have a BoundField being populuated with the "MyDate" column. The MyDate is in a strongly typed DataSet. The...
1
by: Bill44077 | last post by:
I am dynamically adding a checkbox in a gridview and I find there are several things that I cannot figure out how to address. 1. The width of the checkbox column is very wide. I've tried adding...
0
by: landesjoe | last post by:
Hi, here's my problem in short: Text boxes in gridview don't seem to hold their value if the column's .Visible property is changed back and forth. I've got a form with a gridview populated from...
3
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a gridview with several template columns and a few command button columns, column 1 and 2 are dropdown boxes, column 3 is a checkbox and column 4 is a text box, followed by command button...
1
nitindel
by: nitindel | last post by:
Hi All, Please tell me any good site for Gridview control.(not for datagrid). I am facing error in fetching the values of the Bound columns in the gridview: lease tell me how should i...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.