Solved

DataGrid: Align Columns Right

Posted on 2004-10-29
466 Views
Last Modified: 2012-05-05
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.
0
Question by:billy21
    12 Comments
     
    LVL 18

    Expert Comment

    by:tusharashah
    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
     
    LVL 18

    Accepted Solution

    by:
    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
     
    LVL 17

    Expert Comment

    by:AerosSaga
    or in VB
     DataGrid1.Columns(0).ItemStyle.HorizontalAlign = HorizontalAlign.Right
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    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
     
    LVL 6

    Author Comment

    by:billy21
    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
     
    LVL 6

    Author Comment

    by:billy21
    Columns are automatically generated and I use VB .Net.
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    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
     
    LVL 18

    Expert Comment

    by:tusharashah
    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
     
    LVL 6

    Author Comment

    by:billy21
    There is no 'Events' property for the datagrid in my list of properties...
    EdititemIndex
    EditItemStyle
    Enabled
    EnableViewState
    0
     
    LVL 17

    Assisted Solution

    by:AerosSaga
    Look in the codebehind view for the drop downs
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    control---------------                                                                    event------------------------------
    --------------------------------------------------------------------------------------------------------------------------
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    your in design view, go to codebehind
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    857 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now