Solved

manually sorting a datagrid containing an asp:ButtonColumn?

Posted on 2006-07-08
4
351 Views
Last Modified: 2008-02-01
I need to sort a datagrid manually (can't just requery and change the ORDER BY clause).  The code below works perfectly for every column except the asp:ButtonColumn.  How to swap the two button controls?

        'test code to swap row 1 and row 2
        Dim i As Integer
        For i = 0 To dg.Columns.Count - 1
            Dim s As String = dg.Items(1).Cells(i).Text
            dg.Items(1).Cells(i).Text = dg.Items(2).Cells(i).Text
            dg.Items(2).Cells(i).Text = s
        Next

From the .aspx for the button controls:

   <asp:ButtonColumn ButtonType="PushButton" DataTextField="job_number" SortExpression="job_number"
                    HeaderText="Job" CommandName="Select" DataTextFormatString="{0}"></asp:ButtonColumn>


Thanks for any help-
0
Comment
Question by:ottenm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17065213
I don't understand what you are trying to accomplish?  If you are bound to a data source, than apply a sort to the Table.DefaultView.Sort property, and rebind.

Bob
0
 

Author Comment

by:ottenm
ID: 17065268
Sorry if the descripiton was confusing, too much detail.

I need a way to swap two button controls that are sitting in a datagrid.  Any leads?

Thanks for any help-
0
 
LVL 3

Accepted Solution

by:
mdg12 earned 250 total points
ID: 17065378
Like TheLearnedOne says, you could use a DataView if you have access to a DataTable in your BindData method...  How are you originally binding to the data (e.g SqlDataReader, DataTable, etc)??

If you just want an improved version of your original code, you might want to try copying the entire control collection, instead of just the text.. something like (in c#):
      // Get a reference to the datagriditems out here since they don't change
      DataGridItem dgi1 = dg.Items[1];
      DataGridItem dgi2 = dg.Items[2];
      for (int i=0; i<dg.Columns.Count; i++)
      {
            // Initialize arrays to hold the original controls
            TableCell dgc1 = dgi1.Cells[i];
            TableCell dgc2 = dgi2.Cells[i];
            System.Web.UI.Control[] c1 = new System.Web.UI.Control[dgc1.Controls.Count];
            System.Web.UI.Control[] c2 = new System.Web.UI.Control[dgc2.Controls.Count];

            // Copy all of the controls from the cells
            dgc1.Controls.CopyTo(c1, 0);
            dgc2.Controls.CopyTo(c2, 0);

            // Clear out the old controls in each cell
            dgc1.Controls.Clear();
            dgc2.Controls.Clear();

            // Copy back the controls from cell 2 into cell 1
            for (int j=0; j<c1.Length; j++) {
                  dgc1.Controls.Add(c2[j]);
            }

            // Copy back the controls from cell 1 into cell 2
            for (int j=0; j<c1.Length; j++) {
                  dgc2.Controls.Add(c1[j]);
            }
      }
0
 

Author Comment

by:ottenm
ID: 17065526
Perfect!  Thanks.  I had tried to assign the controls and the control array directly with no luck.  Didn't think to look for Clear() and Add().

Again, thanks!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question