align data column from codebehind

  • Thread starter news.microsoft.com
  • Start date
N

news.microsoft.com

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
 
R

Raterus

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
 
A

Amar

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top