Resize Column to fit it's contents

46 posts, 0 answers
  1. Waleed Seada
    Waleed Seada avatar
    241 posts
    Member since:
    May 2006

    Posted 18 Nov 2009 Link to this post

    Dear All,

    I have a gridview control with more than 50 item, how can I display them in the gridview with there full width.
    I have tried AutoResizeMode, but it shrink all of them in the viewable area of the grid, I need that all columns
    get resized to it's contents regardless of it's visuallity.

    Is this possible...

    Best regards
  2. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 23 Nov 2009 Link to this post

    Hi Waleed Seada,

    Thank you for contacting us. I am not sure that I understand your question correctly. However, you can control the column width by using AutoSizeColumnsMode property. When it is set to Fill all columns are resized to fit to the available grid area. When it is set to None, its width is controlled by GridViewColumn.Width property. You can call MasterGridViewTemplate.BestFitColumns method to let RadGridView calculate the optimal width. I hope this helps, if you have further questions, please write me back.

    Regards,
    Jack
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Joshua Teter
    Joshua Teter avatar
    1 posts
    Member since:
    Aug 2009

    Posted 09 Mar 2010 Link to this post

    I am having the same issue.

    Problem: I have a RadGridView that has 30 columns and I want every column to auto size to where the column's content is displayed fully. However, the BestFitColumns mode resizes the columns to where all 30 columns can be seen without scrolling but this makes their content cut off. I want each column to be fit to see it's content even if that means that the user must scroll to the right to see all of the columns. The Fill Mode does the same as BestFitColumns in that it resizes the columns so that they are visible within the grid (with no scroll bars)

    I have attached two files, StandardTelerikLayout.png shows how the fields are displayed. DesiredTelerikLayout.png shows how I need them to show (I got them to show like that by manually double-clicking the header lines to force each column to size-to-fit.). Notice the second picture has a scroll bar, so each column isn't visible within the grid without scrolling but instead each column's size is changed to fits it's own contents.

    Thanks!

    -Josh
  5. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 10 Mar 2010 Link to this post

    Hi Joshua Teter,

    I am not sure that I understand the issue correctly. From what you described, I think that the best option is to call BestFitColumns method. Please send me your application, this will allow me to research the issue further. I am looking forward to your reply.

    Regards,
    Jack
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  6. Roland
    Roland avatar
    156 posts
    Member since:
    Feb 2009

    Posted 17 Jun 2010 Link to this post

    Hello,

    What I need is to be able to set one column to Fill and all others fixed width.

    How do I do that ?
  7. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 23 Jun 2010 Link to this post

    Hi Roland,

    You can achieve this by setting MinWidth and MaxWidth properties for all columns that should have fixed size and by using fill mode. Consider the sample below:

    for (int i = 0; i < this.radGridView1.Columns.Count - 1; i++)
    {
        this.radGridView1.MasterGridViewTemplate.Columns[i].MinWidth = 60;
        this.radGridView1.MasterGridViewTemplate.Columns[i].MaxWidth = 60;
        this.radGridView1.MasterGridViewTemplate.Columns[i].Width = 60;
    }
     
    this.radGridView1.MasterGridViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;

     

    Best wishes,
    Jack
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. Phillip Foster
    Phillip Foster avatar
    58 posts
    Member since:
    Apr 2010

    Posted 16 Jul 2010 Link to this post

    Hi. I want to achieve a similar effect, but i don't want a fixed size on the other columns, because i don't know what the width is going to be. 

    I want to be able to say something like :
    dataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
    dataGridView.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
    dataGridView.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
    dataGridView.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;


    thus making the first 3 columns fit to the size of the largest text, and the forth to fill the rest of the grid. 

    Is this possible with the RadGridView?

    Thanks. 
  9. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 20 Jul 2010 Link to this post

    Hi Phillip Foster,

    This feature is not available in RadGridView. However, we plan to extend our column sizing modes and I will add this as a feature request. It will be considered when planning our future versions and if more people request the same feature, we will raise its priority.

    Should you have any other questions, don't hesitate to ask.

    Sincerely yours,
    Jack
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  10. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 22 Sep 2010 Link to this post

    What Phillip requested is what I am trying to do as well.

    I have financial data and a "description" column. I want to ensure that the full contents of the financial data are shown and grow/shrink the "description" column so needed. If the user resizes the form, the financial data columns should stay the same size (fit to contents) and the "description" column show grow or shrink.

    So there is no work around for this at this point in time?
  11. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 24 Sep 2010 Link to this post

    Hello everyone,

    Sadly there is a strange thing in the radGridView1.Columns[0].BestFit(); in my tests if you have a pyramidal structure for the data contained in the column, let's say in column 0 we have 10 rows and there is a letter on the first, row, 2 on the second, and so on, if you change the size of the column to cover most of the text in the column and create a button that calls just BestFit() on the column you will see that it takes more than a few presses until all the content is visible...

    Deborah, if you just have one column that you always need at maximum width, on data binding complete,  you could just go trough all of the rows, get the maximum string size for that column and based on that calculate the min, max & width of that column and just like Jack said, set the grid
    this.radGridView1.MasterGridViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;

    This is not a clean solution, but it passes as a workaround until a fix will be released...

    void radGridView1_DataBindingComplete(object sender, Telerik.WinControls.UI.GridViewBindingCompleteEventArgs e)
            {
                var maxString = string.Empty;
                foreach (var row in radGridView1.Rows)
                {
                    var value = row.Cells[0].Value.ToString();
                    maxString = maxString.Length > value.Length ? maxString : value;
                }
     
                var minWidth = radGridView1.CreateGraphics().MeasureString(maxString, radGridView1.Font);
                radGridView1.Columns[0].MinWidth = (int)minWidth.Width + 5;
                radGridView1.Columns[0].MaxWidth = (int)minWidth.Width + 5;
                radGridView1.Columns[0].Width = (int)minWidth.Width + 5;
            }

    Hope this helps, and hope that the problem with BestFit() on the column will be fixed soon.

    Best Regards,
    Emanuel Varga
  12. Bernd Mueller
    Bernd Mueller avatar
    38 posts
    Member since:
    May 2010

    Posted 24 Sep 2010 Link to this post

    Hello all,

    that problem was troubling me as well and i would like to join the feature request fraction to raise the priority of the task.
    Thank you and have a nice weekend!

    Bernd
  13. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 24 Sep 2010 Link to this post

    There is another issue I see. I have a bound grid of customers and a bound grid of data associated with the customer. As the user picks another customer, the second grid is rebound.

    When the page first comes up, the columns correctly fill the grid. However, when I rebind the second grid, the spacing is no longer correct. It looks like it may be providing space for a scroll bar? (But my grid is a standard # of rows and columns, so there should never need to be a scroll bar.)

    Bottom line, it leaves a white column of empty space between the last column of data and the edge of the grid which is not acceptable to my customer for their product. Again, this only happens AFTER a rebind. It works fine when it is first displayed on the screen.

    The only way around this I found was to clear the grid columns and rebuild them (instead of just rebinding the data in the columns.)

    So if you could look into this one as well I would appreciate it.

    Thanks!
  14. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 24 Sep 2010 Link to this post

    Hello Deborah,

    Thank you for this explanation. I understand what you want to achieve. You should use the AutoSizeColumnsMode set to Fill. Before setting this property set the desired column size for the financial column and set its AllowResize property to false. This will fix it size and it will not change when RadGridView grows/shrinks. Here is a sample:

    this.radGridView1.Columns["Value"].Width = 120;
    this.radGridView1.Columns["Value"].AllowResize = false;
    this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;

    I hope this helps. If you have any further questions, do not hesitate to ask.

    Best wishes, Jack
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  15. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 24 Sep 2010 Link to this post

    Thank you for getting back to me so quickly.

    We are getting closer, but that won't quite work for me. The user can change the font for the application (just like the little font size area in IE). So if the font changes, the grid columns do need to resize. With my original settings, this was working fine (until I rebound the grid). With these new settings, that no longer works.

    My current work around is just to delete all of the columns and rebuild the grid, which works, but can't be all that efficient.

    Thanks again!
  16. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 25 Sep 2010 Link to this post

    Hello Deborah,

    Did you read my reply from earlier?, there, on data binding complete, you will calculate the maximum size of the column based on it's content using the Font on the grid, when the user changes the font, the font on the grid will change also, so this will continue to work, try it and please let me know....

    Just put that in a method and call it also when the user changes the font, register for the FontChanged event and call it from there, it should do what you need

    Best Regards,
    Emanuel Varga
  17. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 28 Sep 2010 Link to this post

    Yes, I have tried just about everything. I really think that there is a problem with the grid because it continues to look like it is saving a space for the scroll bar.

    When I added code to hard-code the column width, it is even more apparent that it is attempting to save that space. I attached a screen shot.
  18. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 29 Sep 2010 Link to this post

    Hello Deborah,

    I've tried to reproduce your issue, by using the same 2 grids one for data, and one for details that loads when CurrentRowChanged in the main grid.

    I will attach here the test I've made, please let me know what is different in your scenario.

    namespace TestGridSourceDetails
    {
        using System;
        using System.ComponentModel;
        using System.Text;
        using System.Windows.Forms;
        using Telerik.WinControls.UI;
     
        public partial class Form1 : Form
        {
            private RadGridView radGridViewMain;
            private RadGridView radGridViewChild;
     
            public Form1()
            {
                InitializeComponent();
                this.Size = new System.Drawing.Size(800, 600);
                this.Load += new EventHandler(Form1_Load);
            }
     
            void Form1_Load(object sender, EventArgs e)
            {
                radGridViewMain = new RadGridView();
                radGridViewMain.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
                radGridViewMain.CurrentRowChanged += new CurrentRowChangedEventHandler(radGridViewMain_CurrentRowChanged);
                radGridViewMain.DataSource = new TestsCollection(10);
                radGridViewMain.Dock = DockStyle.Left;
                radGridViewMain.Size = new System.Drawing.Size(this.Width / 2 - 10, this.Height);
     
                radGridViewChild = new RadGridView();
                radGridViewChild.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
                radGridViewChild.Dock = DockStyle.Right;
                radGridViewChild.Size = new System.Drawing.Size(this.Width / 2 - 10, this.Height);
     
                this.Controls.Add(radGridViewMain);
                this.Controls.Add(radGridViewChild);
            }
     
            void radGridViewMain_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
            {
                var currentItem = (Test)radGridViewMain.CurrentRow.DataBoundItem;
                radGridViewChild.DataSource = new TestDataCollection(currentItem.Id, 10);
            }
        }
     
        #region Helpers
     
        public class Test
        {
            public Test(int id, string name)
            {
                this.Id = id;
                this.Name = name;
            }
     
            public int Id
            {
                get;
                set;
            }
     
            public string Name
            {
                get;
                set;
            }
        }
     
        public class TestData
        {
            public TestData(int id, int testId, string name)
            {
                this.Id = id;
                this.TestId = testId;
                this.Name = name;
            }
     
            public int Id
            {
                get;
                set;
            }
     
            public int TestId
            {
                get;
                set;
            }
     
            public string Name
            {
                get;
                set;
            }
        }
     
        public class TestsCollection : BindingList<Test>
        {
            public TestsCollection(int noItems)
            {
                for (int i = 0; i < noItems; i++)
                {
                    this.Add(new Test(i, RandomString.GetRandomString(DateTime.Now.Millisecond, new Random(7).Next(10, 20))));
                }
            }
        }
     
        public class TestDataCollection : BindingList<TestData>
        {
            public TestDataCollection(int testId, int noItems)
            {
                for (int i = 0; i < noItems; i++)
                {
                    this.Add(new TestData(i, testId, RandomString.GetRandomString(DateTime.Now.Millisecond, new Random(7).Next(10, 20))));
                }
            }
        }
     
        internal static class RandomString
        {
            public static string GetRandomString(int initialSeed, int size)
            {
                StringBuilder builder = new StringBuilder();
                Random random = new Random(initialSeed);
                char ch;
                for (int i = 0; i < size; i++)
                {
                    ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                    builder.Append(ch);
                }
     
                return builder.ToString();
            }
        }
     
        #endregion Helpers
    }

    Best Regards,
    Emanuel Varga
  19. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 01 Oct 2010 Link to this post

    Hi Debora, I understand your concerns and we will consider implementing this feature in a future version of RadGridView. Please, check the solution from Emanuel. I think that it will fit in your case. If you need further assistance, I will be glad to help.
     
    Sincerely yours,
    Jack
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  20. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 16 Oct 2010 Link to this post

    After much more testing of this ... it appears that the problem is with BestFitColumns. If I add BestFitColumns to any of my grids that currently work, I then get that same extra space (see my prior screen shot) that looks like the grid is saving space for a scroll bar.

    If I take BestFitColumns out, it appears to be stopping this behavior.
  21. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 16 Oct 2010 Link to this post

    I decided to try to implement this code that you provided using MeasureString. It does not work in the following cases:

    • The cell is wrapped (it attempts to fit the entire contents instead of allowing the wrap to occur)
    • The cell is formatted (it ignores the formatting, causing the size to be inaccurate)
    • The cell's header is longer than its text.
    • The cell's header length is not longer, but its measure size is (example: phone numbers since numbers are often narrower than letters in the header)
    • The cell is bound to a combobox (it uses the Value and not the DisplayValue).
      • I tried using the .Text property, but it does not appear to support that like the ASP.Net version seems to have.

    I have found work arounds for the first four, but am really stuck on the last one. Here is my code:

    Private Sub SCStarDataGrid_DataBindingComplete(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewBindingCompleteEventArgs) Handles Me.DataBindingComplete
        If Me.Rows.Count > 0 Then
            Dim largestString As String
            For Each col In Me.Columns
                ' For any wrap columns, set a default minimum width
                If col.WrapText = True Then
                    col.MinWidth = 20
                    col.Width = 20
                Else
                    ' Start with the header text
                    largestString = col.HeaderText
                    For Each row In Me.Rows
                        Dim cellValue = If(row.Cells(col.Index).Value Is Nothing, String.Empty, row.Cells(col.Index).Value.ToString)
                        ' Format the cell value based on the provided format string
                        If Not String.IsNullOrWhiteSpace(col.FormatString) Then
                            ' The date formatting only works if the data type is a date, so try to convert it.
                            Dim cellDate As DateTime
                            If DateTime.TryParse(cellValue, cellDate) Then
                                cellValue = String.Format(col.FormatString, cellDate)
                            Else
                                cellValue = String.Format(col.FormatString, cellValue)
                            End If
                        End If
                        largestString = If(largestString.Length >= cellValue.Length, largestString, cellValue)
                    Next
                    Dim columnSize = Me.CreateGraphics().MeasureString(largestString, Me.Font)
                    Dim columnHeaderSize = Me.CreateGraphics.MeasureString(col.HeaderText, Me.Font)
                    Dim columnWidth = If(columnSize.Width > columnHeaderSize.Width, columnSize.Width, columnHeaderSize.Width)
                    col.MinWidth = CType(columnWidth, Integer) + 5
                    col.Width = CType(columnWidth, Integer) + 5
                End If
            Next
        End If
    End Sub


    Any help would be appreciated.

  22. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 16 Oct 2010 Link to this post

    Hello Deborah,

    I have a solution for you, but it's not a clean one, but it will work in all possible cases, and you don't have to use that complicated calculation function, but I'm not sure if it's the cleanest way to do things, but it will work.

    From the previous posts, i know you are creating a custom grid control, and this is good because this solution also requires you to hold a dictionary of strings.
    The actual .Text property of a cell is just available in the CellFormatting event and in the RowFormatting event,
    Please take a look at the following example:
    C#
    Dictionary<string, string> dictionary = new Dictionary<string, string>();
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        var column = e.CellElement.ColumnInfo;
        var text = e.CellElement.Text;
        if (string.IsNullOrEmpty(text))
        {
            return;
        }
         
        if (dictionary[column.HeaderText].Length < text.Length)
        {
            dictionary[column.HeaderText] = text;
            column.MinWidth = (int)(this.CreateGraphics().MeasureString(text, this.Font).Width+8);
        }
    }

    and VB:
    Private dictionary As New Dictionary(Of String, String)()
    Private Sub radGridView1_CellFormatting(sender As Object, e As CellFormattingEventArgs)
        Dim column = e.CellElement.ColumnInfo
        Dim text = e.CellElement.Text
        If String.IsNullOrEmpty(text) Then
            Return
        End If
     
        If dictionary(column.HeaderText).Length < text.Length Then
            dictionary(column.HeaderText) = text
            column.MinWidth = CInt(Me.CreateGraphics().MeasureString(text, Me.Font).Width + 8)
        End If
    End Sub

    The idea here is to hold a dictionary with the column header text as the key and the largest string on that column as the value, and setting the minimum width on the column based on that value.

    Please let me know what you think about this.

    Best Regards,
    Emanuel Varga
  23. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 16 Oct 2010 Link to this post

    I forgot about the DataBindingComplete event in my previous example, where you first add the header texts to the dictionary, here is the complete code with a fixed min + max width for all the columns:

    again, C#
    Dictionary<string, string> dictionary = new Dictionary<string, string>();
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        var column = e.CellElement.ColumnInfo;
        var text = e.CellElement.Text;
        if (string.IsNullOrEmpty(text))
        {
            return;
        }
     
        if (dictionary[column.HeaderText].Length < text.Length)
        {
            dictionary[column.HeaderText] = text;
            column.MaxWidth = (int)(this.CreateGraphics().MeasureString(text, this.Font).Width + 8);
            column.Width = column.MaxWidth;
            column.MinWidth = column.MaxWidth;
        }
    }
     
    void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
    {
        var grid = sender as RadGridView;
        foreach (var column in grid.Columns)
        {
            dictionary.Add(column.HeaderText, column.HeaderText);
            column.MaxWidth = (int)(this.CreateGraphics().MeasureString(column.HeaderText, this.Font).Width + 8);
            column.Width = column.MaxWidth;
            column.MinWidth = column.MaxWidth;
        }
    }

    and VB:
    Private dictionary As New Dictionary(Of String, String)()
    Private Sub radGridView1_CellFormatting(sender As Object, e As CellFormattingEventArgs)
        Dim column = e.CellElement.ColumnInfo
        Dim text = e.CellElement.Text
        If String.IsNullOrEmpty(text) Then
            Return
        End If
     
        If dictionary(column.HeaderText).Length < text.Length Then
            dictionary(column.HeaderText) = text
            column.MaxWidth = CInt(Me.CreateGraphics().MeasureString(text, Me.Font).Width + 8)
            column.Width = column.MaxWidth
            column.MinWidth = column.MaxWidth
        End If
    End Sub
     
    Private Sub radGridView1_DataBindingComplete(sender As Object, e As GridViewBindingCompleteEventArgs)
        Dim grid = TryCast(sender, RadGridView)
        For Each column As var In grid.Columns
            dictionary.Add(column.HeaderText, column.HeaderText)
            column.MaxWidth = CInt(Me.CreateGraphics().MeasureString(column.HeaderText, Me.Font).Width + 8)
            column.Width = column.MaxWidth
            column.MinWidth = column.MaxWidth
        Next
    End Sub

    Again, please let me know if this is ok for you,

    Best Regards,
    Emanuel Varga
  24. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 18 Oct 2010 Link to this post

    Hello again,

    @Deborah i was wondering if the solution i offered helped you solve your issues?

    Best Regards,
    Emanuel Varga
  25. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 18 Oct 2010 Link to this post

    Thank you for following up.

    I am in California ... so I just got to work a few minutes ago. I will try this later this morning.

    My biggest concern in just looking at the code is performance. The Formatting event is generated 100's of times.

    Longer term, will you be adding a .Text or .FormattedValue property so we can access the shown value of the column outside of the Formating event?

    Thanks again!
  26. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 18 Oct 2010 Link to this post

    Hello again,

    I know that the CellFormatting event is fired all the time, a maybe slightly faster option would be the RowFormatting event, but i am just trying to offer some workarounds that will do what you require at least until this issue will be fixed.

    Please let me know if this works as expected.

    Best Regards,
    Emanuel Varga
  27. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 18 Oct 2010 Link to this post

    Hmmm. No. I am not able to get this to work in all cases.

    It seems that setting the .Width property in the DataBindingComplete causes the CellFormatting event to occur in some cases.

    This is what is happening:
    1) DataBindingComplete resizes the first column based on the column header size.
    2) This causes the CellFormatting event on the second column, so the code generates an exception because it is trying to update the text in a Dictionary entry that has not yet been defined ("The given Key was not present in the dictionary.")

    So, I assume I have several options here:
    1) Modify the code in the CellFormatting to add the dictionary entry if it does not exists yet. I would also have to change the code then in DataBindingComplete to do the same thing so I don't overwrite the value set up in the CellFormatting event.
    2) I need to initialize the dictionary without setting the column widths yet and instead set the column widths in the CellFormatting event.
    3) ??? Other options?
  28. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 18 Oct 2010 Link to this post

    I was able to get past this problem by removing all width setting code from the DataBindingComplete event and just have it resize on formatting.

    I do have one other problem, however. If the user enters a large amount of text, one column can overwhelm the grid, forcing the other columns to become too small or off the screen. The client does not want the column to wrap or to scroll, but instead to show an ellipsis if the column text is too long.

    So I assume I also need to add to this some calculation to ensure that the total widths of all columns does not exceed the amount of space given to the grid on the form. Any tips or links on this calculation?
  29. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 18 Oct 2010 Link to this post

    Hello again, something like the windows table layout panel approach, a maximum percentage / column, set based on the importance of that column in the grid, and before setting the MinWidth, check that percentage.

    And at least like that you can provide an interface for the user to customize the grid based on his/her needs, and use a Save / Load layout to load the settings for the grid, usually more customizations => happier customers.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  30. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 18 Oct 2010 Link to this post

    Wow. An awful lot of work for something that already works this way in the DataGridView that somes with Visual Studio. :-(

    Any code examples to kickstart this? (I have two weeks to finish the application and can't afford to spend 1 week of it getting the grids to work correctly. So any tips for how to write all of this code quickly would be appreciated.)
  31. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 18 Oct 2010 Link to this post

    The first thing I am trying to do is figure out what the maximum width is that I am shooting for. When I display one of my grids, the column widths total 631. But the grid width is coming out to be 906. On the screen the columns take up 100% of the grid. Are these in different units?

    Or how do I determine the maximum width I can fill with the columns?

    Thanks!
Back to Top
UI for WinForms is Visual Studio 2017 Ready