1

I have a Form with a single DGV snapped into the center of the form.

Is it possible to set the form to automatically adjust it's width (upto a maximum limit) to accomodate the DGV - dependent on the total width of the fields in the DGV.

Do I need to use a similar methodology to this post or is this possible 'out of the box' using control properties?

Community
  • 1
  • 1
whytheq
  • 34,466
  • 65
  • 172
  • 267
  • Are you using a simple form or an Mdi Container? – SoEnLion May 14 '12 at 16:22
  • WPF can do that kind of thing automatically. – Kendall Frey May 14 '12 at 16:34
  • If you dock the DGV, you can use the Form.AutoSize and AutoSizeMode properties to make the form automatically size itself to the grid. Sizing the grid to its columns is another matter. – Igby Largeman May 14 '12 at 18:31
  • @KendallFrey maybe it's a question I should put to SO but when does one use a WPF rather than a UserForm - is WPF more web orientated? – whytheq May 14 '12 at 18:40
  • Use WPF when you want to stay with current technology. Nothing wrong with WinForms, but WPF is better. If you have an existing project in WinForms, don't bother converting, but MS recommends WPF for all new desktop software development. – Kendall Frey May 14 '12 at 18:43
  • thanks Kendall ... I was going to put this as a question but I suspect it would be difficult to phrase correctly and not get [Closed] – whytheq May 14 '12 at 20:46

2 Answers2

1

EDIT:

public Form1()
{
    InitializeComponent();

    dataGridView1.AutoSize = true;
    dataGridView1.SizeChanged += new EventHandler(dataGridView1_SizeChanged);
    dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
}

void dataGridView1_SizeChanged(object sender, EventArgs e)
{
    Width = dataGridView1.Width + 10;
}
ispiro
  • 26,556
  • 38
  • 136
  • 291
  • @whytheq I assumed you know enough to do the rest as in Brett's answer. – ispiro May 14 '12 at 18:40
  • columns won't be getting added – whytheq May 14 '12 at 18:44
  • text will be getting entered into cells in one of the fields and in some of the cells the text could be greater than the max text string already in this field of the DGV – whytheq May 14 '12 at 18:45
  • suppose I can just pick another event for when new rows are added to the DGV – whytheq May 14 '12 at 18:51
  • @whytheq I tried it before posting it. But not sure that's what you're looking for. Notice, it will only resize when you press Enter or click on another cell. – ispiro May 14 '12 at 19:18
  • ok - might be good for it to respond on every key press as I'm entering info into the cell – whytheq May 14 '12 at 19:25
0

You could set the DataGridView to size fit it's contents by setting the 'AutoSizeColumnsMode' and 'AutoSizeRowsMode' property to 'AllCells':

dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells

From here, you can resize your form to fit the DataGridView by setting the size of the form with the Size of the control:

this.Height = dataGridView.Height;
this.Width = dataGridView.Width;

So I suppose you could check if contents were added or removed to the DataGridView by handling the 'ColumnWidthChanged' event (since you only care about width), and do the form resize in that handler.

B L
  • 1,572
  • 1
  • 14
  • 26