none
How can I programmatically autosize columns in Datagrid?

    Question

  • I am using VS2003. In a datagrid, when I display a dataset, it comes with few columns. When I double-clicking section separator (= autosize column) it automatically extends the column to show max width of the column.

    How can I programmatically double-click autosize event so that all columns are displayed autosized level.C#, C++ or VB examples or link or examples are appreciated.

    Thanks in advance,
    Wednesday, April 26, 2006 6:03 AM

Answers

  • You may wanna fine out which row has the longest username, then determine its index in the DataGrid. With the index calculate the requirement cell width with the follow example

    http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q877q
    Thursday, August 24, 2006 8:12 AM
  • Do you have the option to additionally run a Max length query on the result set and determine the index of that row while in still in SQL? then you can possibly return that as an output parameter which the DataGrid can then quickly zoom in to calculate the graphical length.

    If your application layers have already been defined and developed by other people, such architecture most likely won't be flexible enough to accomodate this which is almost purely a presentation-layer concern.
    Friday, September 01, 2006 2:21 AM

All replies

  •  

    You can do this by using the DataGridTableStyle.

    • First instantiate a new datagrid table style.
    • set the mapping name
    • add this style to the data grid
    • assign the width for columns

     

    DataGridTableStyle dgts = new DataGridTableStyle();

    dgts.MappingName = "StyleName";

    DataGrid1.TableStyles.Add(dgts);

    DataGrid1.TableStyles["StyleName"].GridColumnStyles["ColumnName"].Width = 100

    DataGrid1.TableStyles["StyleName"].GridColumnStyles["ColumnName1"].Width = 50

     

    Try with this....

     

    Thnkx

    Pesani

    Thursday, April 27, 2006 9:10 AM
  • Hi,

    Is your problem solved? If yes, then please mark it as answered.

    Thank you,
    Bhanu.

    Wednesday, May 03, 2006 7:02 PM
  • Hi, using column style we can do it, I understand. However, the values like 100 or 50 shown above depends on the data. Supposing list 10000 user_names from a dataset. some may have 10 chars and max may be 30 or 40 chars width. As per this logic, I have to loop until 10000 lines and find max of it, use it in style.However, when you attach a dataset with a datagrid, you go manually double click like excel column, it automatically expends to the max width. Can I simulate this action without going through 10k loop logic?Thanks,
    Thursday, August 24, 2006 5:24 AM
  • You may wanna fine out which row has the longest username, then determine its index in the DataGrid. With the index calculate the requirement cell width with the follow example

    http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q877q
    Thursday, August 24, 2006 8:12 AM
  • icelava ,

    Looks like this is the only option for me to loop till the end.

    Thanks,

    Tuesday, August 29, 2006 8:55 AM
  • Do you have the option to additionally run a Max length query on the result set and determine the index of that row while in still in SQL? then you can possibly return that as an output parameter which the DataGrid can then quickly zoom in to calculate the graphical length.

    If your application layers have already been defined and developed by other people, such architecture most likely won't be flexible enough to accomodate this which is almost purely a presentation-layer concern.
    Friday, September 01, 2006 2:21 AM