Saturday 30 May 2009

Move Command Link Column to End Column – Dynamic Data

Whilst answering this question Move Edit, Details, Delete (col 0) to the Rightmost column of table on the Dynamic Data Forum I found this had been answered before (Move GridView command column or dynamically append columns) and decided to post it to my blog in full for easy access.

The post suggest using IAutoFieldGenerator to add the command column to the end like this:

public ICollection GenerateFields(Control control)
    // Get all of table's columns, take only the ones that should be automatically included in a fields collection,
    // sort the result by the ColumnOrderAttribute, and for each column create a DynamicField
    var fields = from column in _table.Columns
                 where IncludeField(column)
                 orderby ColumnOrdering(column), column.Name
                 select new DynamicField()
                     DataField = column.Name,
                     HeaderText = column.DisplayName

    List<DynamicField> flds = fields.ToList();
    if (_table.PrimaryKeyColumns.Count > 0)
        // get the first primary key field                
        DynamicField ctrl = new DynamicField();
        ctrl.HeaderText = "Commands";
        ctrl.DataField = _table.PrimaryKeyColumns[0].Name;
        ctrl.UIHint = "GridCommand";

    return flds;

Listing 1 – GenerateFields method of the IAutoFieldGenerator

The main change to my usual IAutoFieldGenerator is shown in Bold Italic this works by only adding the GridCommand FieldTemplate if the table a=has a Primary Key.

    NavigateUrl='<%# Table.GetActionPath(PageAction.Edit, Row) %>' 
    Text="Edit" />
    OnClientClick='return confirm("Are you sure you want to delete this item?");' />
    NavigateUrl='<%# Table.GetActionPath(PageAction.Details, Row) %>' 
    Text="Details" />

Listing 2 – GridCommand FieldTemplate

In Listing 2 we see the GridCommand FieldTemplate layed out neatly (you may need to tidy it so that it is similar to the section it is copied form in the List page. >&nbsp;<)


The download contains the full IAutoFieldGenerator and GridCommand FieldTemplate plus Column sort capability.


Shaheen said...

Thanx Stephen!! this is exactly wot i was looking for but am having one problem my list view pages are working just perfectly fine, I have two tables Prosucts and Products Warranties in Warranty Table i have a composite key of ProductId and Duration. My problem is on the Product Warranty insert page instead of showing *view, Edit, Delete* in Command column it showing me a numeric field of my forien key column. How am i suppose to fix that.

Thanx, Hope to get a reply from you soon

Stephen J. Naughton said...

Hi Shaheen, could you send me an e-mail with a screen shot of the problem and I'll see if I can help.

Steve :D

Shaheen said...

Hey Steve!! thanx for ur reponse, Got ur email id from ur blog and sent u the screenshot from my gmail id.

Unknown said...

Hi Steve i've got some question, how to hide those action links on Edit.aspx?

Stephen J. Naughton said...

Hi Adrian, how do you mean do you just want to do it per table?

Unknown said...

I just don't wanna see action links when i go to edit page. On List.aspx one of actions redirect to edit page, but i don't wanne see this action on Edit.aspx

Stephen J. Naughton said...

Then you could just edit the Edit.aspcx page and remove them :)

Steve :D

Anonymous said...


Your solution only works with List.aspx page :)
You forgot to put the same logic in ListDetails.aspx :)

It could be useful to update your solution..

Thank you for your post,

Stephen J. Naughton said...

Sorry I never use the ListDetails page :(