auto size columns

Posted by: parez81-fp on 8 September 2017, 1:12 pm EST

  • Posted 8 September 2017, 1:12 pm EST

    Hi,

    I have a  grid where i populate the grid using the following code.


     foreach (Collection row in value)
                    {
                        colIndex = 0;
                        foreach (string rowitem in row)
                        {
                            ui_financialInfo_Sheet1.SetText(rowIndex, colIndex, rowitem);

                            colIndex++;
                        }
                        rowIndex++;
                    }

    and set the column headers using the following..

     foreach (string columnHeader in value)
                    {
                        ui_financialInfo_Sheet1.Columns[value.IndexOf(columnHeader)].Label = GetFormattedDate(columnHeader);
                    }


    I want the the columns to automatically resize based on the width of the text. it is turned on by default when you use databinding but show do i do it when i manually assign values to the grid
  • Replied 8 September 2017, 1:12 pm EST

    Try setting DataAutoSizeColumns to false and then use GetPreferredColumnWidth to set the size of the columns.
  • Replied 8 September 2017, 1:12 pm EST

    Hello,


    I think you have put this code in the wrong location. I would suggest adding the line of code for setting the width until after setting the text to the ColumnHeader label.


                        ui_financialInfo_Sheet1.Columns[value.IndexOf(columnHeader)].Label = GetFormattedDate(columnHeader);

  • Replied 8 September 2017, 1:12 pm EST

            public  Collection ColumnHeader
            {
                get
                {
                    throw new NotImplementedException();
                }
                set
                {
                    foreach (string columnHeader in value)
                    {
                        ui_financialInfo_Sheet1.Columns[value.IndexOf(columnHeader)].Label = GetFormattedDate(columnHeader);
                        ui_financialInfo_Sheet1.Columns[value.IndexOf(columnHeader)].Width = ui_financialInfo_Sheet1.GetPreferredColumnWidth(value.IndexOf(columnHeader), false);
                    }
                }
            }

            public  Collection> DataRows
            {
                get
                {
                    throw new NotImplementedException();
                }
                set
                {
                    //  throw new NotImplementedException();

                   // ui_financialInfo_Sheet1.Models.Data = new FarPoint.Win.Spread.Model.DefaultSheetDataModel(value, typeof(Collection));
                    //return;
                    int rowIndex = 0;
                    int colIndex = 0;
                    ui_financialInfo_Sheet1.DataAutoSizeColumns = true;

                    foreach (Collection row in value)
                    {
                        colIndex = 0;
                        foreach (string rowitem in row)
                        {
                            ui_financialInfo_Sheet1.SetText(rowIndex, colIndex, rowitem);
                          //  ui_financialInfo_Sheet1.Columns[colIndex].Width = ui_financialInfo_Sheet1.GetPreferredColumnWidth(colIndex, false);
                            colIndex++;
                            
                        }
                        rowIndex++;
                    }
                     
                }
            }


    I have the above code..

    First i Assign data ColumnHeader property
    and then i assign DataRows property..

  • Replied 8 September 2017, 1:12 pm EST

    And the auto sizing didnt work..
  • Replied 8 September 2017, 1:12 pm EST

    Hello,


    By default, WordWrap is turned on in the headers and the GetPreferredColumnWidth will not size any larger, since the text is wrapped. Have you turned off the WordWrap in the header?

     
    		Dim ecr As New FarPoint.Win.Spread.CellType.EnhancedColumnHeaderRenderer
    ecr.WordWrap = False
    FpSpread1.Sheets(0).ColumnHeader.DefaultStyle.Renderer = ecr
    FpSpread1.Sheets(0).Columns(1).Label = "This is a very long text title."
    FpSpread1.Sheets(0).Columns(1).Width = FpSpread1.Sheets(0).GetPreferredColumnWidth(1, False)
     
  • Replied 8 September 2017, 1:12 pm EST

    Thanks.. that worked...
  • Replied 8 September 2017, 1:12 pm EST

    Hi,

      foreach (Collection row in value)
                    {
                        colIndex = 0;
                        foreach (string rowitem in row)
                        {
                            ui_financialInfo_Sheet1.SetText(rowIndex, colIndex, rowitem);
                            ui_financialInfo_Sheet1.Columns[colIndex].Width = ui_financialInfo_Sheet1.GetPreferredColumnWidth(colIndex, false);
                            colIndex++;
                           
                        }
                        rowIndex++;
                    }

    i used the above code and it didnt work.

    In the request that i am working on right now.
    One of the column header is bigger than all row items for that column
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels