Datagridview multi line textbox when ctrl+enter pr

2019-03-01 21:09发布

I'm trying to add some specific behavior to a datagridview control (if you can put it like that).

I would like to have a multiplelines textbox control in the datagridview. I achieved this when giving a string that is too long to fit in the textbox, it's automatic placed on a second line.

So the steps would be enter text into a cell, while the focus is on the cell press ctrl+enter so that the text entered next is on a second line.

dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells

But I would also be able to force a second line by pressing ctrl + enter key On this matter my internet search came up empty.

I guess it would be catching the key press event within a cell but I can't seem to find this event.

Possible events

I tried this event, but it only happens when the Datagridview in total is selected.

Private Sub DataGridView1_CellContentClick(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DataGridView1.KeyPress
     ' Some magic here.
End Sub

2条回答
我想做一个坏孩纸
2楼-- · 2019-03-01 21:22

Sorry for the C# syntax. But I guess the event exists also in VB.NET.

I would suggest to use the KeyDown event:

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.Enter)
    {
        MessageBox.Show("I have been Executed!");
    }
}

this works for me catching keyboard input when the DataGridView has focus.

查看更多
时光不老,我们不散
3楼-- · 2019-03-01 21:33

I am fairly confident you can achieve the desired results by changing a couple of properties in the DataGridView and use a “Shift-Enter” instead of “Ctrl-Enter”.

In the DataGridView set its AutoSizeRowsMode to AllCells, then set the particular COLUMNS DefaultCellType -> WrapMode to true. Then use a “Shift-Enter” while editing a cell and you should get multiple lines in a cell. Hope this is what you were looking for.

查看更多
登录 后发表回答