C # datagridview column autosizemode

I want the default columns to use

AutoSizeMode = DisplayedCells; 

but I also want to resize the columns, but the DisplayedCells type does not allow resizing.

any ideas?

+4
source share
5 answers

I do not think that you can achieve this because AutoSizeMode is once installed on DisplayedCells, all behavior is controlled by design. But I have this idea. You need to leave your column (I suppose columns [0] for demo purpose). AutoSizeMode fixed in DataGridViewAutoSizeColumnMode.None . You want to set it as DisplayedCells , because you may need to expand the width of the column or collapse it depending on the length of the cell text. So my idea is that every time CellBeginEdit starts, we set AutoSizeMode to DisplayedCells, and when CellEndEdit starts, we save the Width (which is authorized for you) before switching from AutoSizeMode to None , then assign the column width to this saved value. Here is my code:

 //First before loading data private void form_Load(object sender, EventArgs e){ dataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; //Fill your dataGridView here //......... //......... int w = dataGridView.Columns[0].Width; //reset to None dataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; dataGridView.Columns[0].Width = w; } //Now for CellBeginEdit and CellEndEdit private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { if(e.ColumnIndex == 0) //because I suppose the interested column here is Columns[0] dataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; } private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if(e.ColumnIndex == 0){ int w = dataGridView.Columns[0].Width; dataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; dataGridView.Columns[0].Width = w; } } 

I tested the code and it seemed to work fine, there is a case that it will not work, because we are not adding code for this case, that is, when the Cell value is changed by the code.

I have to say that your desire is a little strange, I am not too worried about the column width, the user needs to know how to work with it.

+1
source

You can call sub DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells) when it is convenient, for example, after loading the data. Leave the DataGridView.AutoSizeColumnsMode property yourself, and the user will still be able to resize the columns themselves, but it will be convenient for them to start. The best of both worlds.

+6
source

Row:

 dataGridView1.AutoResizeRow(2, DataGridViewAutoSizeRowMode.AllCellsExceptHeader); 

Column:

 dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; 
+1
source

In one of my applications, I have an autosize function for displayed cells. Then, once the form is loaded, I will turn off auto-detection so that users can perform calibration.

 private void Form1_Load(object sender, EventArgs e) { // Designer has autosize set to displayedcells. dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None; // Turn off autosize dataGridView1.AllowUserToResizeRows = true; // Turn on letting user size columns dataGridView1.AllowUserToOrderColumns = true; } 
0
source

The only thing that worked for me in Visual Studio 2008 (and VB.net) was:

  For i As Integer = 0 To grdList2.Columns.Count - 1 If i <> (grdList2.Columns.Count - 1) Then grdList2.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells Else grdList2.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill End If Next 
0
source

Source: https://habr.com/ru/post/1485067/


All Articles