right aligning values in datagrid column

N

nemo

I'm creating a datasource and populating a datagrid at runtime. The
code is as follows:

dt = New DataTable

dt.Columns.Add(New DataColumn("Item", GetType(String)))
dt.Columns.Add(New DataColumn("Value", GetType(String)))

For i = 0 To j - 1
dr = dt.NewRow()
dr(0) = (i + 1) & "."
dr(1) = (SomeArray(i).Amount).ToString("c")

dt.Rows.Add(dr)
Next

Question: The second column contains $ amount and I need to right align
the values in that column. How do I do it?

thanks a lot
 
S

S. Justin Gengo

Nemo,

Is the code you're showing for the datagrid?

It looks like it's just the datasource's code...

Right alignment would be done in the grid itself.

Instead of letting the grid autogenerate the columns you'll need to declare
each column yourself and tell it what to bind to.

Your finished grid would look something like this:

<asp:datagrid id="DataGrid1" runat="server" autogeneratecolumns="False"
width="100%">
<Columns>
<asp:BoundColumn DataField="Item" HeaderText="Item
Column"></asp:BoundColumn>
<asp:BoundColumn DataField="Value" HeaderText="Value Column"
ItemStyle-HorizontalAlign="Right"></asp:BoundColumn>
</Columns>
</asp:datagrid>

Note that the DataField is the column name to bind to in the datasource and
the autogeneratecolumns is set to false.
It's the BoundColumn's ItemStyle that lets you set horizontal alignment (And
many others).

In your codebehind you bind to the grid the same way.

DataGrid1.DataSource = [Your DataSource Here]
DataGrid1.DataBind()


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top