This is a migrated thread and some comments may be shown as answers.

change text allignment for autogenerated columns

2 Answers 24 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 22 Jan 2014, 11:03 PM
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 Answers, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 1
answered on 24 Jan 2014, 04:26 PM
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.
0
Hristo
Telerik team
answered on 27 Jan 2014, 08:58 AM
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 >>
Tags
GridView
Asked by
Kevin
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or