[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 494
  • Last Modified:

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.
0
billy21
Asked:
billy21
  • 5
  • 4
  • 3
2 Solutions
 
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
 
AerosSagaCommented:
or in VB
 DataGrid1.Columns(0).ItemStyle.HorizontalAlign = HorizontalAlign.Right
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now