3

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).

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Mirrana
  • 1,601
  • 6
  • 28
  • 66
  • there are several options.. CSS is one of them.. what do you have so far in regards to code..? have you tried anything such as sizing the separate column itself as well as setting the auto generate columns property to false.. – MethodMan Jan 10 '12 at 15:42
  • 1
    he is talking about Windows Forms, not Asp.NET – dknaack Jan 10 '12 at 15:48

1 Answers1

0
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;
    }    
}
ispiro
  • 26,556
  • 38
  • 136
  • 291
  • Wow, that works pretty much exactly how I wanted it. The only discrepancy (which is mostly just me nitpicking) is that the column doesn't resize in real time. I have to release the mouse for it to actually update. Do you know of a way to change this so that the column resizes as I drag it? – Mirrana Jan 10 '12 at 19:55
  • @agent154 Nope. Sorry. (I don't mean it can't be done. _I_ don't know how.) – ispiro Jan 10 '12 at 20:21