change text allignment for autogenerated columns

3 posts, 0 answers
  1. Kevin
    Kevin avatar
    12 posts
    Member since:
    Oct 2013

    Posted 22 Jan 2014 Link to this post

    Hi, I have a telerik rad gridview that auto-generates columns.  I want to change formats, fonts and text alignments for specific columns.  Right now I have the following code:

    private void CustomerDataGrid_AutoGeneratingColumn(object sender, Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs e)
            {
                if (e.Column.Header.ToString() == "dummy")//WSS
                {
                    ((GridViewDataColumn)this.CustomerDataGrid.Columns["trade_Date"]).DataFormatString = "{0:d}";
                    ((GridViewDataColumn)this.CustomerDataGrid.Columns["value_Date"]).DataFormatString = "{0:d}";
                    ((GridViewDataColumn)this.CustomerDataGrid.Columns["amt_Bought"]).DataFormatString = "{0:N2}";
                    ((GridViewDataColumn)this.CustomerDataGrid.Columns["amt_Sold"]).DataFormatString = "{0:N2}";
     
                    ((GridViewDataColumn)this.CustomerDataGrid.Columns["amt_Bought"]).TextAlignment = TextAlignment.Right;
                    ((GridViewDataColumn)this.CustomerDataGrid.Columns["amt_Sold"]).TextAlignment = TextAlignment.Right;
     
     
                    ((GridViewDataColumn)this.CustomerDataGrid.Columns["deal_Number"]).Header = "Deal Number";
                    ((GridViewDataColumn)this.CustomerDataGrid.Columns["trade_Date"]).Header = "Trade Date";
                    ((GridViewDataColumn)this.CustomerDataGrid.Columns["value_Date"]).Header = "Value Date";
                    ((GridViewDataColumn)this.CustomerDataGrid.Columns["tran_Type"]).Header = "Tran Type";
                    ((GridViewDataColumn)this.CustomerDataGrid.Columns["cross"]).Header = "Cross";
                    ((GridViewDataColumn)this.CustomerDataGrid.Columns["CCY_Bought"]).Header = "CCY Bought";
                    ((GridViewDataColumn)this.CustomerDataGrid.Columns["amt_Bought"]).Header = "Amt Bought";
                    ((GridViewDataColumn)this.CustomerDataGrid.Columns["CCY_Sold"]).Header = "CCY Sold";
                    ((GridViewDataColumn)this.CustomerDataGrid.Columns["amt_Sold"]).Header = "Amt Sold";
                    ((GridViewDataColumn)this.CustomerDataGrid.Columns["CCY_Bought"]).Header = "CCY Bought";
     
                    e.Column.IsVisible = false;
                }
            }

    I've noticed a few things - it cant edit columns that is the current column or it wont display, hence I manually add a extra column called "dummy" then edit the columns that are already generated.

    Also, and this is my main concern, text alignment doesn't seem to be working .. the code runs by the text alinement for that column is still left justified. 

    Can you provide a sample code that changes the text alignment for a single column?

    thanks
  2. Kevin
    Kevin avatar
    12 posts
    Member since:
    Oct 2013

    Posted 24 Jan 2014 Link to this post

    hi can someone please help?  I am not sure why my code it not working, searching the internet gives me the exact same code i used.  I also tried making a style and setting it for the column.cellstyle, but it made every column right justified instead.  I am very confused.
  3. DevCraft banner
  4. Hristo
    Admin
    Hristo avatar
    41 posts

    Posted 27 Jan 2014 Link to this post

    Hi,

    You can solve your issue during AutoGeneratingColumn event by setting the event's column property TextAlingment. Also I believe that, it would be better if you use switch ... case instead of dummy column.
    I'm attaching a sample project demonstrating this approach.

    Regards,
    Hristo
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top