Results 1 to 2 of 2

Thread: Column Width in Datagrid

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2004
    Posts
    8

    Angry Column Width in Datagrid

    I'm trying to use the .Width property of a DataGridColumnStyle to change the width, but the width will only go to the Datagrid PreferredColumnWidth. Does anyone know what properties to set or code that will change the column widths individually?

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: Column Width in Datagrid

    Are you using the DataGridColumnStyle class appropriately?
    Example:
    Code:
             // Create an example DataTable called "Example"
             DataTable table = new DataTable("Example");
             
             // Add a few String fields
             table.Columns.Add("Column1", typeof(string));
             table.Columns.Add("Column2", typeof(string));
             table.Columns.Add("Column3", typeof(string));
             table.Columns.Add("Column4", typeof(string));
             table.Columns.Add("Column5", typeof(string));
    
             // Add a few rows of example data
             table.Rows.Add(new object[] { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" });
             table.Rows.Add(new object[] { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" });
             table.Rows.Add(new object[] { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" });
             table.Rows.Add(new object[] { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" });
             table.Rows.Add(new object[] { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" });
    
             // Bind the "Example" table to the DataGrid
             dataGrid1.DataSource = table;
    
             // Create a new DataGrid TableStyle
             DataGridTableStyle style = new DataGridTableStyle();
             // Map the TableStyle to the "Example" table
             style.MappingName = "Example";
             // Add the TableStyle to the Grid, at which point it creates the
             // Individual ColumnStyles for each column in the table
             dataGrid1.TableStyles.Add(style);
             
             // Alter the width of a specific column/columns
             style.GridColumnStyles["Column1"].Width = 10;
             style.GridColumnStyles["Column3"].Width = 150;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width