Friday, July 6, 2012

Align Text in Autogenerated Column of Rad Silverlight Girdview and Silverlight Gridview

Shout it kick it on DotNetKicks.com
In this post I am going to show how you can align data in autogenrated columns cell of Silverlight gridview and also of Rad Control Silverlight Gridview.
In both of the below Example of Gridview I want to align the numeric data left in my cell and other except numeric remain in same format.

RAD Silverlight Gridview
XAML code of Silverlight Gridview
<telerik:RadGridView  
         Name="clubsGrid" 
         ItemsSource="{Binding Clubs}"
 AutoGeneratingColumn="clubsGrid_AutoGeneratingColumn"
         Margin="5">
</telerik:RadGridView>
Thing to note down here in XAML code is I register AutoGeneratingColumn =
"clubsGrid_AutoGeneratingColumn" event which is get called when Auto columns get generated for gridview.
private void clubsGrid_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
{
 GridViewDataColumn column = e.Column as GridViewDataColumn;
        if (column.DataType == typeof(int)
           || column.DataType == typeof(decimal)
           || column.DataType == typeof(float)
                )
       {
           column.TextAlignment = TextAlignment.Right;
       }
 }
As you see in above code I attached event called AutoGeneratingColumn on gridview control and checking DataType of each column which in turn check the datatype of the property which going to be attached with that column. So when the DataType is int or decimal or float I set TextAlignment propery of column to Right so display numeric value in right.

Output
So the output shows the column with numeric value "StadiumCapacity" is align to right.

Silvelight SDK Gridview control
There are two way to achieve this in Silverlight gridview
     1) Setting cell style from code behind file but creating Style
     2) Setting cell style from code behind file but using resource 
XAML code of Silverlight Gridview
<sdk:DataGrid   IsReadOnly="True" 
       Name="mysGrid" 
      AutoGeneratingColumn="DataGrid_AutoGeneratingColumn"
       ItemsSource="{Binding Clubs, Mode=OneWay}">
</sdk:DataGrid>
Same as RAD Gridview here also wrote AutoGeneratingColumn="DataGrid_AutoGeneratingColumn" which take care of Autogenerated coulmn.

First Way : Creating Sytle
 private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
 {
    if (e.PropertyType == typeof(int)
        || e.PropertyType == typeof(decimal)
        || e.PropertyType == typeof(float)
       )
    {
        var rightCellStyle = new Style(typeof(DataGridCell)); 
               
        rightCellStyle.Setters.Add(new Setter(
             Control.HorizontalContentAlignmentProperty,
             HorizontalAlignment.Right));

        DataGridBoundColumn obj = e.Column as DataGridBoundColumn;
        obj.CellStyle = rightCellStyle;
     }
}
As you see in above code same as RAD gridview control here e.PropertyType used to check the type of the autogenerated column but the change over here is need to create cell style and than assing the style to CellStyle property of gridview column.

Second Way : Using Resource
In this solution you need to register the style for the gridview cell as shown below and than you can use this to assign to CellStyle.
Resource in App.XAML
  <Style x:Key="RightCellStyle" TargetType="sdk:DataGridCell">
      <Setter Property="HorizontalContentAlignment" Value="Right" />
  </Style>
CodeBehind file
private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyType == typeof(int)
        || e.PropertyType == typeof(decimal)
        || e.PropertyType == typeof(float)
       )
       {
           DataGridBoundColumn obj = e.Column as DataGridBoundColumn;
           var rightCellStyle = Application.Current.Resources["RightCellStyle"] as Style;
           obj.CellStyle = rightCellStyle;
       }
}
Now in this code you dont require to create any style you just need to fetch the resource that you register in the App.XAML file and need to convert in Style.

Output
So the output shows the column with numeric value "StadiumCapacity" is align to right.
Note : in both the way output remain same.

19 comments:

  1. I have been surfing online greater than 3 hours as of late, but I never found any interesting article
    like yours. It is beautiful value sufficient for me.
    In my opinion, if all webmasters and bloggers made good content as you did, the net
    will probably be a lot more helpful than ever before.

    My blog post ... http://library.mopartrucksandstuff.com/

    ReplyDelete
  2. If you are going for best contents like I do, just
    pay a visit this website all the time as it gives
    quality contents, thanks

    Also visit my web-site :: Natural Weight Loss

    ReplyDelete
  3. And that's the only reason we're in the gym, right.
    Just make sure that whatever routine you decide on that you do it on a regular
    basis so you can achieve the best results.
    One mistake that people who want to build muscles quickly but have no information is the lack of information
    regarding how much they should eat in order to grow muscles.


    Here is my webpage; Order Power Pump XL

    ReplyDelete
  4. I'm not sure exactly why but this web site is loading extremely slow for me. Is anyone else having this problem or is it a problem on my end? I'll check back later
    on and see if the problem still exists.

    Feel free to visit my web-site :: raspberry ketones reviews

    ReplyDelete
  5. You should take part in a contest for one of the best blogs on the internet.
    I most certainly will recommend this website!

    Nutri green

    ReplyDelete
  6. Thank you, I've just been looking for information approximately this subject for a while and yours is the best I've came upon so far.
    However, what concerning the conclusion? Are you sure
    concerning the source?

    Feel free to visit my webpage - Pureberry Max Trial

    ReplyDelete
  7. Appreciating the time and energy you put into your website and
    in depth information you present. It's nice to come across a blog every once in a while that isn't
    the same out of date rehashed material. Excellent read!
    I've bookmarked your site and I'm including your RSS
    feeds to my Google account.

    My webpage Natural Cleanse REviews

    ReplyDelete
  8. I like what you guys tend to be up too. This type of clever
    work and reporting! Keep up the good works guys I've included you guys to blogroll.

    work from home

    ReplyDelete
  9. It's truly very complicated in this active life to listen news on Television, thus I just use web for that reason, and obtain the most up-to-date news.

    Feel free to surf to my website: Advanced Test-o-boost Review

    ReplyDelete
  10. Thank you for sharing your info. I truly appreciate your efforts and
    I will be waiting for your next write ups thank you once again.


    Also visit my web-site; Lift and glow pro review

    ReplyDelete
  11. can any one please tell me how to display an underline style for the content in few columns of the grid

    ReplyDelete
  12. Hi there exceptional website! Ɗoes running ɑ blog liҟe tҺis require a lаrge amօunt oof worҝ?
    I've absolutely no understanding of programming hоwever I Һad bеen hoping to start my own blog in tɦe near future.
    Аnyways, should yоu hаve any ideas oг tips for
    new blog owners ρlease share. І know tɦis is off topic howеνer I simply wanted
    to ask. Cheers!

    Also visit my homepage; 100 pure garcinia cambogia walmart

    ReplyDelete
  13. Wow,,, very good articel,, thx for your information

    ReplyDelete
  14. Good information, thanks ... please visit my website Kartika Putri

    ReplyDelete
  15. wow , is so very good article, thanks for information sir , but don't forgot visit my blog > www.klubindojob.com

    ReplyDelete
  16. Thank you for sharing your info. I truly appreciate your efforts and
    I will be waiting for your next write ups thank you once again.
    Also visit my webpage Shopping Ipad

    ReplyDelete
  17. PHP nya kok sulit ya? ada yang bisa bantu?
    tanyatania.com

    ReplyDelete
  18. Please you explain what is the meaning of the RAD Silverlight Gridview and XAML code of Silverlight Gridview

    ReplyDelete
  19. Can explain how easy it is to create it, when you want a vacation please visit to bali

    ReplyDelete