Frage

I wish that by default the columns uses the

AutoSizeMode = DisplayedCells;

but I wish also the possibility to resize the columns, but DisplayedCells type doesn't allow to resize..

any ideas?

War es hilfreich?

Lösung 2

I don't think you can achieve that because the AutoSizeMode once is set to DisplayedCells all the behavior is controlled by design. But I have this idea. You should have to keep your column (I suppose Columns[0] for demonstrative purpose) AutoSizeMode fixed at DataGridViewAutoSizeColumnMode.None. You want to set it as DisplayedCells because you may want the column width to expand or collapse depending on the Cell text length. So my idea is every time the CellBeginEdit starts, we set the AutoSizeMode to DisplayedCells and when the CellEndEdit starts we save the Width (which is autosized for you) before reseting the AutoSizeMode to None, then assign the column Width to that 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 seems to work OK, there is a case that it won't work because we don't add code for that case, that is when the Cell value is changed by code.

I have to say that your want is a little strange, I don't care too much about the column's width, user should know how to do with it.

Andere Tipps

You can call the sub DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells) whenever it's convenient, such as after you've loaded the data. Leave the DataGridView.AutoSizeColumnsMode property alone and the user will still be able to resize the columns themselves, but they'll have a comfortable start. Best of both worlds.

Row:

dataGridView1.AutoResizeRow(2, DataGridViewAutoSizeRowMode.AllCellsExceptHeader);

Column:

dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

In one of my applications, I have autosize set to displayedcells. Then once the form is loaded, I turn off autosize in order to allow the users to do the sizing.

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;
    }

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top