Flex Grid Subtotal Alignment

WinForms

ComponentOne's WinForms controls

Flex Grid Subtotal Alignment

  • rated by 0 users
  • This post has 3 Replies |
  • 1 Follower
  • I am using the flex grid subtotal feature. I would like to change the alignment so that when I do a "count" in text field the total will be right alignment but the underlying data cells will be left aligned.  Is there a way to do this?

     thanks

  • The easy way to do this is to change the column's alignment to one of the "general" settings. This will cause the grid to align string content to the left and numeric content to the right (like Excel does). By default, string columns are aligned to the left and numeric columns are aligned to the right.

    The code would look like this:

                // create some data
                var dt = new DataTable();
                dt.Columns.Add("Name", typeof(string));
                foreach (string s in "one,two,three,four".Split(','))
                {
                    dt.Rows.Add(s);
                }
    
                // bind grid
                this.c1FlexGrid1.DataSource = dt;
    
                // set column alignment to 'general center'
                this.c1FlexGrid1.Cols["Name"].TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.GeneralCenter;
    
                // add a subtotal to show that this works            
                this.c1FlexGrid1.Subtotal(C1.Win.C1FlexGrid.AggregateEnum.Count, 0, -1, 1);
    

    A more flexible solution would be to assign custom styles directly to the subtotal cells. This would not affect the regular cells in any way, and you could change any style elements (not just the alignment). Here's the code you would use to implement this option:

        public partial class Form1 : Form
        {
            // declare the custom style to be applied to subtotal cells
            C1.Win.C1FlexGrid.CellStyle  _rightAlignStyle;
            
            public Form1()
            {
                InitializeComponent();
    
                // create some data
                var dt = new DataTable();
                dt.Columns.Add("Name", typeof(string));
                foreach (string s in "one,two,three,four".Split(','))
                {
                    dt.Rows.Add(s);
                }
    
                // bind grid
                this.c1FlexGrid1.DataSource = dt;
    
                // create custom style for subtotal cells
                _rightAlignStyle = this.c1FlexGrid1.Styles.Add("rightAlign");
                _rightAlignStyle.TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.RightCenter;
    
                // hook up event handler to assign custom style to subtotal cells
                this.c1FlexGrid1.AfterSubtotal += c1FlexGrid1_AfterSubtotal;
                
                // add a subtotal to show that this works            
                this.c1FlexGrid1.Subtotal(C1.Win.C1FlexGrid.AggregateEnum.Count, 0, -1, 1);
            }
    
            void c1FlexGrid1_AfterSubtotal(object sender, C1.Win.C1FlexGrid.SubtotalEventArgs e)
            {
                // assign custom style to subtotal cells
                c1FlexGrid1.SetCellStyle(e.NewRowIndex, 1, _rightAlignStyle);
            }
    
    

    I hope these solutions work for you.

  • Thanks very much.  I used the second solution and it seems to be working well. 

    I think I should have mentioned that my application is in VS2008/VB using LINQ to create data for my binding source.

     

  • Glad to be of help. Thanks for the feedback.
Page 1 of 1 (4 items)