Move GridView command column or dynamically append columns

Last post 02-04-2009 11:03 PM by ricka6. 3 replies.

Sort Posts:

  • Move GridView command column or dynamically append columns

    02-03-2009, 6:06 PM
    • Member
      point Member
    • 10Bulls
    • Member since 02-03-2009, 9:51 PM
    • Posts 2

    Greetings all,  I am hoping you can save my crumbling sanity.

    I have written a simple Dynamic Data application, displaying a scaffolded table to a GridView.  So far so good.

     

    All I would like to do is move the command column, with Edit Delete Insert etc (column(0) to the right most column.

    I could do this by writing Custom Pages for each object, but I would like to do this for the generic PageTemplate.

     

    My thought was to create the command column in code and append it to the column collection but I am struggling with this.

    I have tried creating a column in Page_Load, GridView_DataBound and GridView_PreRender events, but in each case the columns collection is empty at that point and the new column still ends up on this left.

    When is the GridView ColumnsGenerator called?  I put a break point in a custom field generator but the call stack was not helpful.

    Thanks for your time and sorry if this has come up before, but hours of searching have drawn a blank.

     

     

     

     

  • Re: Move GridView command column or dynamically append columns

    02-03-2009, 11:02 PM
    Answer
    • Contributor
      2,162 point Contributor
    • davidfowl
    • Member since 08-17-2008, 9:50 PM
    • Redmond
    • Posts 446
    • AspNetTeam
      Moderator

    The only way to do this would be to create your own IAutoFieldGenerator and add the command field at the end.

    David Fowler
    SDE, ASP.NET Team, Microsoft
  • Re: Move GridView command column or dynamically append columns

    02-04-2009, 4:53 AM
    Answer
    • Member
      point Member
    • 10Bulls
    • Member since 02-03-2009, 9:51 PM
    • Posts 2

    Thank you very much for the quick reply and yes that worked.

    Here is a snippet from my field generator ( adapted from DynamicDataFutures )

    I'm not sure if this is the best way to go about it, but it seems to work and leaves me to get on beating my head against the next obstacle. Confused

    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)
    select new DynamicField() {
    DataField = column.Name,
    HeaderText = column.GetDisplayName() // use extension method to refetch display name to work around localization bug
    };


    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";
    flds.Add(ctrl);
    }

    return flds;
    }

    Where GridCommand.ascx is a field template
    <%@ Control Language="C#" CodeBehind="GridCommand.ascx.cs" Inherits="DynamicTest.GridCommand" %>
    <asp:DynamicHyperLink ID="DetailsHyperLink" runat="server" Text="Details" />
     

     

  • Re: Move GridView command column or dynamically append columns

    02-04-2009, 11:03 PM
    • Contributor
      5,362 point Contributor
    • ricka6
    • Member since 06-25-2008, 6:04 PM
    • Redmond
    • Posts 892
    • AspNetTeam
      Moderator

    Hey 10bulls,

    Thanks for posting your code. David tells me your solution is fine.

    Rick -ASP.Net UE  Rick on MVC & Dynamic Data   
Page 1 of 1 (4 items)