Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
693 views
in Technique[技术] by (71.8m points)

.net - DataGridView: How can I make the enter key add a new line instead of changing the current cell?

How can I make the Enter key behave in a Winforms DataGridViewTextBoxCell like it does in a normal Winforms TextBox (add a new line to the text, instead of changing the current cell)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well, I found out how to solve the problem. First, create a class called CustomDataGridViewTextBoxEditingControl which derives from DataGridViewTextBoxEditingControl, and override EditingControlWantsInputKey like this:

public class CustomDataGridViewTextBoxEditingControl : DataGridViewTextBoxEditingControl
    {
        public override bool EditingControlWantsInputKey(
        Keys keyData, 
        bool dataGridViewWantsInputKey)
        {
            switch (keyData & Keys.KeyCode)
            {
                case Keys.Enter:
                    // Don't let the DataGridView handle the Enter key.
                    return true;
                default:
                    break;
            }

            return base.EditingControlWantsInputKey(keyData, dataGridViewWantsInputKey);
    }
}

This stops the DataGridView from handing the Enter key and changing the current cell. It does not, however, make the Enter key add a new line. (It appears that the DataGridViewTextBoxEditingControl has removed the functionality of the Enter key). Therefore, we need to override OnKeyDown and implement the functionality ourselves, like this:

protected override void OnKeyDown(KeyEventArgs e)
{
    switch (e.KeyCode & Keys.KeyCode)
    {
        case Keys.Enter:
            int oldSelectionStart = this.SelectionStart;
            string currentText = this.Text;

            this.Text = String.Format("{0}{1}{2}",
                currentText.Substring(0, this.SelectionStart),
                Environment.NewLine,
                currentText.Substring(this.SelectionStart + this.SelectionLength));

            this.SelectionStart = oldSelectionStart + Environment.NewLine.Length;
            break;
        default:
            break;
    }

    base.OnKeyDown(e);
}

Then, create a class called CustomDataGridViewTextBoxCell which derives from DataGridViewTextBoxCell, and override the EditType property to return the type of CustomDataGridViewTextBoxEditingControl.

public class CustomDataGridViewTextBoxCell : DataGridViewTextBoxCell
{
    public override Type EditType
    {
        get
        {
            return typeof(CustomDataGridViewTextBoxEditingControl);
        }
    }
}

After you do this, you can set the CellTemplate property on an existing column to a CustomDataGridViewTextBoxCell, or you can create a class derived from DataGridViewColumn with CellTemplate preset to a CustomDataGridViewTextBoxCell, and you will be all set!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...