473,699 Members | 2,311 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically added DataGrid Columns and SortExpression

4 New Member
I have a Web page with a datagrid for display search results. The first column contains a check box and a hidden field, and are defined with the datagrid.

Expand|Select|Wrap|Line Numbers
  1.                  <asp:DataGrid runat="server" ID="dgPaused" AutoGenerateColumns="false" AllowPaging="false" AllowSorting="true" cellspacing="0" cellpadding="3" rules="all" border="1" style="border-collapse:collapse;" OnItemDataBound="dgPaused_ItemDataBound" OnSortCommand="dgPaused_SortCommand" >
  2.                     <HeaderStyle CssClass="FixedDataGridHeader" />
  3.                     <ItemStyle />
  4.  
  5.                     <Columns>
  6.                         <asp:TemplateColumn SortExpression="XXX" HeaderText="XXX">
  7.                             <ItemTemplate>
  8.                                 <asp:CheckBox runat="server" ID="chkUnpause" />
  9.                                 <asp:HiddenField runat="server" ID="txtID" Value='<%# DataBinder.Eval(Container.DataItem, "BPID Reference")%>' />
  10.                             </ItemTemplate>
  11.                         </asp:TemplateColumn>
  12.                     </Columns>
  13.                 </asp:DataGrid>                  
  14.  
  15. The remaining fields are added dynamically:
  16.  
  17.             // Build the header row.
  18.             for (int i = 0; i < oDataSet.Tables[0].Columns.Count; i++)
  19.             {
  20.                 string sColumnName = oDataSet.Tables[0].Columns[i].ColumnName;
  21.                 if (sColumnName != "BPID Reference")
  22.                 {
  23.                     BoundColumn oNewColumn = new BoundColumn();
  24.                     oNewColumn.HeaderText = sColumnName;
  25.                     oNewColumn.DataField = sColumnName;
  26.                     oNewColumn.SortExpression = sColumnName;
  27.  
  28.                     if (sColumnName == "Last Modified" || sColumnName == "Pending Time")
  29.                         oNewColumn.DataFormatString = "{0: HH:mm M/d/yyyy}";
  30.                     else
  31.                         oNewColumn.DataFormatString = "{0}";
  32.  
  33.                     dgPaused.Columns.Add(oNewColumn);
  34.                 }
  35.             }
  36.  
Now, please note that sorting is enabled, and clicking on the header field should execute the function “dgPaused_SortC ommand”. Why is it that only the original column, “XXX” actually executes the function? Clicking on any other header just returns to “Page_Load”.

I believe I am writing the code exactly as every example I have found.
Aug 19 '08 #1
0 1414

Sign in to post your reply or Sign up for a free account.

Similar topics

1
8906
by: Webgour | last post by:
Hi, I'm tring to add a column to a datagrid with a linkbutton as header that can be used to sort the column. The column and the linkbutton are added programmatically (see below). However the problem is that when you click the added column header it doesn't trigger the sort. The code : <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
2
8490
by: Ken Tucker | last post by:
I've read about this issue in many articles across the net... But haven't found a solution. I see all kinds of custom code to perform sorting with datagrids, but my example is so simple, I must just be missing something. Basically, I have a .aspx page that is very simple and has a simple datagrid. All code is in the .cs page for the datagrid (hence, I'm doing all datagrid work programmatically). The datagrid is populated from a SQL table...
0
1380
by: Robert Brinson | last post by:
Hello all! I'm running .NET Framework 1.1 using VS.NET 2003. I've got a mystery with a DataGrid. Below is the definition of the DataGrid from my aspx page: </asp:datagrid><asp:datagrid id="dgDetails" Runat="server" Width="100%" Visible="False" AllowSorting="True" CellPadding="1" CellSpacing="0" AutoGenerateColumns="False" OnSortCommand="SortDetails"> <ItemStyle Font-Size="XX-Small" Font-Names="Tahoma"></ItemStyle> <HeaderStyle...
6
4637
by: JenHu | last post by:
Hi experts, I want to add a delete button in my datagrid, and before it deletes from database, I want to have a confirm dialog box for this deletion. I am entry level to the vb.net, and don't know java or c#. The problem is, when I tested the program, there is no pop-up confirmation box for the deletion, can you show me what' wrong with this code? Thank you.
5
2785
by: tshad | last post by:
Is there a way to carry data that I have already read from the datagrid from page to page? I am looking at my Datagrid that I page through and when the user says get the next page, I have to go to the database to get the next page. Is there a way to use the dataset to allow us to read back and forth in it instead of going back to the database to get it? Thanks,
2
3679
by: Luis Esteban Valencia Muñoz | last post by:
I have a datagrid that displays editable text fields (2 different price fields) and a checkbox in every row. It has a "SaveChanges" button at the bottom, which, when pressed, looks at every checkbox in the datagrid, if it is checked, it updates the corresponding rows prices. That works, no problem. What I need to do is that when a user clicks into either of the price text fields, the checkbox automatically checks itself. This will save...
2
3438
by: Mike Baugh | last post by:
I am using visual studio 2005 to develop a form using c# I have 3 datagrids on one form. I can set the row color based on a certain value in a column. However this color applies to all 3 datagrida. I would like to set it so that if value of column 3 in datagrid 1 is < 100 set to red, if = 100 set to green if value of column 3 in datagrid 2 is < 90 set to red, if >= 90 set to green if value of column 3 in datagrid 3is < 80 set to red,...
0
2085
by: rupalirane07 | last post by:
Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error: NullReferenceException error Any help........pls urgent ========================================================= <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm3.aspx.vb" Inherits="TestDatagrids.WebForm3"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">...
7
3030
by: Mark B | last post by:
Can someone write some VB.Net example code for me that does this: 1) Creates a gridview control with the results of a SQL stored procedure that has 1 parameter (text) 2) Adds an extra column that displays the result of a VB.Net function that uses a value from an existing column I have spent 4 hours trying to do this after Googling for examples... There are many new concepts that I haven't had much experience with...
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
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
8908
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,...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
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
4374
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...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
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
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.