styling Columns when using AutoGenerateColumns

Discussion in 'ASP .Net Datagrid Control' started by Tim_Mac, Oct 3, 2006.

  1. Tim_Mac

    Tim_Mac Guest

    hi,
    i have an extended Datagrid class, it does nice automatic things like
    providing an excel export feature, sorting, paging etc.
    i would like it to automatically right-align any numeric columns, but i have
    run into a problem where the 'DataGrid.Columns' collection is empty when the
    DataGrid is using AutoGenerateColumns, and i am unable to set the
    ItemStyle.HorizontalAlign property because there are no DataGridColumns to
    access. can anyone think of a way to style the column, without resorting to
    declaring every column in the ASPX?

    here is the code i use in protected override void Render(...)

    // align the numeric columns
    int col=0;
    if(this.AutoGenerateColumns)
    {
    // iterate over dataview table columns
    foreach(DataColumn dc in this.dv.Table.Columns)
    {
    if(Regex.IsMatch(dc.DataType.Name, "Decimal|Double|Int16|Int32|Int64"))
    this.Columns[col].ItemStyle.HorizontalAlign =
    this.AutoAlignNumberColumns;
    col++;
    }
    }

    very grateful for any suggestions. if i use declared columns it does work
    correctly.
    thanks
    tim
    Tim_Mac, Oct 3, 2006
    #1
    1. Advertising

  2. Hello Tim,

    I suppose you are working with VS.NET 2003 (since in VS.NET 2005, we use
    GirdView instead DataGrid). Regaridng the issue, since you have enable the
    AutoGenerateColumns property, the columns will not be added until the
    datagird is bound to data. In the render method, the columns is not
    generated yet. If you move these code to itemdatabound, or databinding,
    will this correct the problem?

    Sincerely,

    Luke Zhang

    Microsoft Online Community Support
    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Luke Zhang [MSFT], Oct 5, 2006
    #2
    1. Advertising

  3. Tim_Mac

    Tim_Mac Guest

    hi Luke,
    thanks for the reply. yes it's a VS 2003 project, i have most of my
    projects upgraded but one or two larger ones are still using 2003.

    unfortunately DataGrid.Columns.Count is 0 at the start and end of the events
    you describe, when AutoGenerateColumns is used. even the OnUnload event has
    an empty columns collection.

    just to cover the obvious question, there are two columns in the datasource
    and they do render in the grid.

    i wouldn't like to get into manually rendering the TR and TD tags of the
    datagrid if this would have performance implications. perhaps there is an
    internal method like GenerateColumns which i could reflect manually? and
    then gain access to the instantiated Columns?

    thanks
    tim



    "Luke Zhang [MSFT]" <> wrote in message
    news:...
    > Hello Tim,
    >
    > I suppose you are working with VS.NET 2003 (since in VS.NET 2005, we use
    > GirdView instead DataGrid). Regaridng the issue, since you have enable the
    > AutoGenerateColumns property, the columns will not be added until the
    > datagird is bound to data. In the render method, the columns is not
    > generated yet. If you move these code to itemdatabound, or databinding,
    > will this correct the problem?
    >
    > Sincerely,
    >
    > Luke Zhang
    >
    > Microsoft Online Community Support
    > ==================================================
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications.
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    > ==================================================
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    >
    >
    Tim_Mac, Oct 5, 2006
    #3
  4. Hello Tim,

    Sorry for previous suggestions not working. There is no such internal
    method for the "column genearation" but we may override the databind method
    of the datagrid control like:

    public override void DataBind()
    {
    base.DataBind();
    // access columns here
    }

    Will this help?

    Sincerely,

    Luke Zhang

    Microsoft Online Community Support
    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Luke Zhang [MSFT], Oct 6, 2006
    #4
  5. Tim_Mac

    Tim_Mac Guest

    hi Hussein,
    that's no problem, here is the overridden DataBind event in my datagrid. my
    extended datagrid class has one or two bugs that make me slow to send you
    the entire class, but if you really want the whole thing that's no problem
    i'll post it.
    the code below uses a DataView object called dv, which is the datasource for
    the grid.

    public override void DataBind()
    {
    base.DataBind();

    int col = 0;
    // iterate over columns defined in grid
    foreach (DataGridColumn dc in this.Columns)
    {
    // the sort expression is the only link between the datagrid column
    and the datatable column, so we use that to access the datatype of each
    column in the grid
    if (dc.SortExpression != "" &&
    this.dv.Table.Columns[dc.SortExpression] != null &&
    Regex.IsMatch(this.dv.Table.Columns[dc.SortExpression].DataType.Name,
    "Decimal|Double|Int16|Int32|Int64"))
    this.Columns[col].ItemStyle.HorizontalAlign =
    HorizontalAlign.Right;
    col++;
    }
    }

    hope this helps
    tim


    "Hussein" <> wrote in message
    news:...
    > Dear Tim,
    >
    > U can give me the code for this project.
    >
    > Thanks,
    > Regards,
    >
    > ---
    > Posted using Wimdows.net Newsgroups - http://www.wimdows.net/newsgroups/
    Tim_Mac, Nov 9, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Mitchell

    Can you hide columns generated with AutoGenerateColumns?

    Jim Mitchell, Jul 17, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    170
    Jim Mitchell
    Jul 17, 2003
  2. AutoGenerateColumns on some columns

    , Jun 14, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    133
  3. Arthur Dent

    AutogenerateColumns and Static columns.

    Arthur Dent, Jun 22, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    166
    Arthur Dent
    Jun 22, 2005
  4. Jerad Rose

    Hiding DataGrid Columns when AutoGenerateColumns="true"

    Jerad Rose, Mar 13, 2007, in forum: ASP .Net Datagrid Control
    Replies:
    7
    Views:
    978
    Jerad Rose
    Mar 14, 2007
  5. Amrut Kale

    Using AutoGenerateColumns to generate Datagrid columns

    Amrut Kale, Sep 29, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    185
    Amrut Kale
    Sep 29, 2003
Loading...

Share This Page