473,406 Members | 2,371 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,406 software developers and data experts.

Dynamically added DataGrid Columns and SortExpression

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_SortCommand”. 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 1387

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

Similar topics

1
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...
2
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...
0
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...
6
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...
5
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...
2
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...
2
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...
0
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...
7
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.