Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

manually sorting a datagrid containing an asp:ButtonColumn?

Posted on 2006-07-08
4
Medium Priority
?
352 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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
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.
Suggested Courses

670 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