DataGridView: Change Edit Control size while editing

后端 未结 7 894
遇见更好的自我
遇见更好的自我 2020-12-20 16:59

in the DataGridView I want the cell size to expand according to the string length when I edit the cell. Excel does the same.

In the DataGridView, w

7条回答
  •  北海茫月
    2020-12-20 17:09

    This was work for me:

    Enable KeyPreview Property of the form and change the body of KeyPress Event of the form to this:

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
                if (e.KeyChar!='\b')   //discard backspace
                {
                  dataGridView1.Columns[0].Width += 5;  //the column's index or name
    
                }
                else
                {
                    dataGridView1.Columns[0].Width -= 5;  //for backspase pressing
                }
    }
    

    you can limit the pressed keys with e.KeyChar ;

提交回复
热议问题