Solved

manually sorting a datagrid containing an asp:ButtonColumn?

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

12 Experts available now in Live!

Get 1:1 Help Now