DataGrid: Align Columns Right

Is it possible to programatically (perhaps through java script) right align columns in a datagrid control?  All of our datagrid controls are populated on the fly.
LVL 6
billy21Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tusharashahCommented:
You can Set it like following:
// You need to set this before you call DataBind() method..
      DataGrid1.Columns[0].ItemStyle.HorizontalAlign = HorizontalAlign.Right;

-tushar
0
tusharashahCommented:
Or if you are dealing with AutoGenerated Column here. In that case you'll have to Align the columns using ItemDataBound event of datagrid.

Inside ItemDataBound of DataGrid just add one line like following..
------------------------------------------------------------------------
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    e.Item.Cells[0].HorizontalAlign = HorizontalAlign.Right;    //for VB modify it like: e.Item.Cells(0).HorizontalAlign = HorizontalAlign.Right
}
------------------------------------------------------------------------

-tushar
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AerosSagaCommented:
or in VB
 DataGrid1.Columns(0).ItemStyle.HorizontalAlign = HorizontalAlign.Right
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

AerosSagaCommented:
lol beat me too it tusharashah;) but here is the above in vb

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
 e.Item.Cells(0).HorizontalAlign = HorizontalAlign.Right
End Sub
0
billy21Author Commented:
The itemdatabound event doesn't appear to be firing.  I placed the following code in the codebehind of my page and inserted a break point.  It never breaks there and nothing happens to my column alignment...

    Private Sub grdBudget_ItemDataBound(ByVal sender As Object, ByVal e As system.Web.UI.WebControls.DataGridItemEventArgs)
        e.Item.Cells(1).HorizontalAlign = HorizontalAlign.Right
    End Sub

0
billy21Author Commented:
Columns are automatically generated and I use VB .Net.
0
tusharashahCommented:
On your Design view.. Click on DataGrid then Click on "Events" in property Window.. (On top panel)

& then double click on OnItemDataBound event from the list...

Right your code inside the newly created Method..

-tushar
0
tusharashahCommented:
Or inside your WebForm check your if you have following line in your InitializeComponent() method..
private void InitializeComponent()
{    
      this.grdBudget.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.grdBudget_ItemDataBound);
      this.Load += new System.EventHandler(this.Page_Load);
}

-tushar
0
billy21Author Commented:
There is no 'Events' property for the datagrid in my list of properties...
EdititemIndex
EditItemStyle
Enabled
EnableViewState
0
AerosSagaCommented:
Look in the codebehind view for the drop downs
0
AerosSagaCommented:
control---------------                                                                    event------------------------------
--------------------------------------------------------------------------------------------------------------------------
0
AerosSagaCommented:
your in design view, go to codebehind
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.