align data column from codebehind

Discussion in 'ASP .Net Datagrid Control' started by news.microsoft.com, Jun 3, 2004.

  1. Hello.
    I've started a datagrid in codebehind and need to have the numeric columns
    right aligned.

    here's the code:
    -----
    Select Case arThis1(1)

    Case "String"

    dt.Columns.Add(New DataColumn(v, GetType(String)))

    Case "Numeric"

    dt.Columns.Add(New DataColumn(v, GetType(Integer)))

    Case "Date"

    dt.Columns.Add(New DataColumn(v, GetType(Date)))

    End Select

    -----
    when the case is numeric, i would like to have that column
    itemstyle-horizontalalign set to right. Is there a way to do this from
    codebehind.

    I've found a way to do this from the itemdatabound event, but I can't help
    but thinking that this is the wrong way as it would be neccessary to set
    global values, etc just to remember which columns, etc need this property.

    Thanks for any assistance you can give.

    Melissa
    news.microsoft.com, Jun 3, 2004
    #1
    1. Advertising

  2. news.microsoft.com

    Raterus Guest

    Not quite sure what you are after, you are adding columns to a datatables, but you want to right-align this data in a DataGrid. I take it you, fill this datatable with whatever you need, then assign this whole datatable to the datasource of the datagrid? Perhaps then your AutoGenerateColumns=True of the datagrid, so you get some pretty non-customized output, which you want to customize just a tad and right align the numbers.

    If that is the case, I would say do it in itemdatabound, keep track of the current index of the columns you added when adding to the datatable, then in itemdatabound, retrieve these, and set the tablecell so it right-aligns.

    Hope this is even close to helpful :)
    --Michael

    "news.microsoft.com" <> wrote in message news:...
    > Hello.
    > I've started a datagrid in codebehind and need to have the numeric columns
    > right aligned.
    >
    > here's the code:
    > -----
    > Select Case arThis1(1)
    >
    > Case "String"
    >
    > dt.Columns.Add(New DataColumn(v, GetType(String)))
    >
    > Case "Numeric"
    >
    > dt.Columns.Add(New DataColumn(v, GetType(Integer)))
    >
    > Case "Date"
    >
    > dt.Columns.Add(New DataColumn(v, GetType(Date)))
    >
    > End Select
    >
    > -----
    > when the case is numeric, i would like to have that column
    > itemstyle-horizontalalign set to right. Is there a way to do this from
    > codebehind.
    >
    > I've found a way to do this from the itemdatabound event, but I can't help
    > but thinking that this is the wrong way as it would be neccessary to set
    > global values, etc just to remember which columns, etc need this property.
    >
    > Thanks for any assistance you can give.
    >
    > Melissa
    >
    >
    Raterus, Jun 3, 2004
    #2
    1. Advertising

  3. news.microsoft.com

    Amar Guest

    When you add the Columns of the Grid one by one , you must have
    AutoGenerateColumns = false, for every BoundColumn you add you can add
    any attribute you want....eg

    BoundColumn bCol = new BoundColumn();
    bCol.DataField = dt.Columns["name"].ColumnName; <-- Your DataTable
    (dt)
    ..
    ..
    ..
    and at the end before you add it to the GridColumnCollection
    bCol.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    Columns.Add(bCol);

    That's it!


    "news.microsoft.com" <> wrote in message news:<>...
    > Hello.
    > I've started a datagrid in codebehind and need to have the numeric columns
    > right aligned.
    >
    > here's the code:
    > -----
    > Select Case arThis1(1)
    >
    > Case "String"
    >
    > dt.Columns.Add(New DataColumn(v, GetType(String)))
    >
    > Case "Numeric"
    >
    > dt.Columns.Add(New DataColumn(v, GetType(Integer)))
    >
    > Case "Date"
    >
    > dt.Columns.Add(New DataColumn(v, GetType(Date)))
    >
    > End Select
    >
    > -----
    > when the case is numeric, i would like to have that column
    > itemstyle-horizontalalign set to right. Is there a way to do this from
    > codebehind.
    >
    > I've found a way to do this from the itemdatabound event, but I can't help
    > but thinking that this is the wrong way as it would be neccessary to set
    > global values, etc just to remember which columns, etc need this property.
    >
    > Thanks for any assistance you can give.
    >
    > Melissa
    Amar, Jun 4, 2004
    #3
    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. Kay
    Replies:
    2
    Views:
    7,124
  2. carlos perez
    Replies:
    0
    Views:
    399
    carlos perez
    Jun 8, 2004
  3. tshad

    text-align vs align

    tshad, Jun 23, 2005, in forum: HTML
    Replies:
    1
    Views:
    1,232
    David Dorward
    Jun 23, 2005
  4. Paul Lautman

    left align within centre align

    Paul Lautman, Mar 3, 2006, in forum: HTML
    Replies:
    3
    Views:
    552
    Paul Lautman
    Mar 3, 2006
  5. Steve Richter
    Replies:
    1
    Views:
    875
    Mark Rae
    Jun 24, 2007
Loading...

Share This Page