473,654 Members | 2,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7990
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
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.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


Jun 14 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6217
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 if it is in front of the value? My guess would be to use a template column but I dont know how to go about this. Any ideas? Below is an example of what the data looks like raw from the database: $456.95 200.89
6
3032
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 current_row_key of Grid1... to the objectDataSource2 parameter? (so that the second grid, gets only the information to do with current row of grid1)
1
9350
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 GridView.Visible = True. I press HIDE and the GridView disappears as expected. After it I press SHOW and the GridView doesn't show.
1
3237
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 SQLDataSource. The GridView contains 3 columns: QuestionNumber, QuestionText, and a TemplateField. Each row in the GridView represents a question in a table (Ex.: "18. Satisfaction Level").
3
6126
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 values of course come from the DataBase. I have a few values in the Database that have NULL as the value for MyDate. Its not ideal, but I don't have control of this one.
1
2863
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 properties and Item Style to the column but it has no effect. Is there someway to size this column to be smaller? I removed the CssClass property and it had no effect on the size as well. 2. The gridview is sortable using javascript. Is there...
0
2420
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 a data view (which in turn is loaded from a manually setup DataTable for testing purposes). One of the columns in the grid is a checkbox that's tied to an event handler that'll change the .Visible property of a column with a text box. That text...
3
311
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 columns. These are all for data entry and although I am populating the dropdown list boxes with an objectdatasource so the user can provide a selection, I am not binding the gridview to anything. The command buttons again are for the user to be...
1
1844
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 fetch the value..of a bound column..?? Below is the code.:
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8482
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8593
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1593
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.