Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

manually sorting a datagrid containing an asp:ButtonColumn?

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
ottenm
Asked:
ottenm
  • 2
1 Solution
 
Bob LearnedCommented:
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
 
ottenmAuthor Commented:
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
 
mdg12Commented:
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
 
ottenmAuthor Commented:
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

Technology Partners: 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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now