Solved

manually sorting a datagrid containing an asp:ButtonColumn?

Posted on 2006-07-08
4
340 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now