right aligning values in datagrid column

  • Thread starter Thread starter nemo
  • Start date Start date
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
 
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
 
Back
Top