Search code examples
c#winformsdatagridviewdatagridviewcolumn

Resizing DataGridView columns without affecting adjacent ones?


I'd like to implement a feature whereby allowing a user to resize a DataGridView column, but without affecting the adjacent columns. I've noticed in many programs that you can resize a specific column, and all the columns to the left or right will stay fixed in size. This is not the default behavior in .NET though, because when I expand a column, the columns to the right side will compress in equal amounts to account for the added size of the old column.

Is there a way to stop this from happening?

EDIT:

I obviously didn't explain that the best, so lets try this again:

I have a DataTable that holds 8 columns, and a bunch of rows. I add that DataTable as the DataSource for the DataGridView. When my form opens up, the 8 columns are spread evenly across the DataTable, which is set to AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;. I want to keep the AutoSizeColumsMode this way. The form or the control will not be resized. Only the columns within the control.

What I want is for a user to be able to resize an individual column (like, say, column 3) without it affecting the size of all the columns to the right of it. Default behavior is to resize all the other columns to accomodate for the new change. However, I would like it to only resize the column immediately to the right. This way, it won't screw up any custom sizes that were set on column 8.

I saw an application do this properly earlier today, though I wish I could remember which it was so I could take a screenshot. All I know is that when I resized one column in the middle of the table, it didn't change the width of all the columns to the right (only the one immediately next to it).


Solution

  • public partial class Form1 : Form
    {    
        int[] w = new int[100];
        public Form1()
        {
            InitializeComponent();
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;    
            Shown += new EventHandler(Form1_Shown);
        }
    
        void Form1_Shown(object sender, EventArgs e)
        {
            dataGridView1.ColumnWidthChanged += dataGridView1_ColumnWidthChanged;
    
            //to know what WERE the widths.
            for (int i = 0; i < dataGridView1.Columns.Count; i++) w[i] = dataGridView1.Columns[i].Width;
    
            //Won't work without this.
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
        }    
    
        void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
        {
            int column = e.Column.Index;
    
            //unsubscribe so changing will not call itself.
            dataGridView1.ColumnWidthChanged -= dataGridView1_ColumnWidthChanged;
    
            //updating width.
            dataGridView1.Columns[column + 1].Width = (w[column + 1] + w[column]) - dataGridView1.Columns[column].Width;
            w[column + 1] = dataGridView1.Columns[column + 1].Width;
            w[column] = dataGridView1.Columns[column].Width;
    
            //re-subscribe
            dataGridView1.ColumnWidthChanged += dataGridView1_ColumnWidthChanged;
        }    
    }