DataGridView Editing in multiline mode

How do I make the DataGridView show multiline text when it is being edited?
I added:

dataGridView1.Columns[1].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

That only shows multiline after editing is complete.

10

As I understand it, you’d like DataGridView to behave in the manner we’re all accustomed to in MS Excel where you can see all of the lines of a multiline entry during editing, before commit and the row height adjustment that results.


What I reproduced from your code is that only the last line of a multiline entry is visible during editing, but appears normally after commit.


Here’s one way that worked for me in testing. Ordinarily, the underlying cell value isn’t changing when the text in the editing control does. The value is copied on a commit, and cancelling the operation simply skips the copy operation.

But by changing the underlying cell value in response to changes in the editor control, then the row height tracks because of the AutoSizeRows setting. If the edit is cancelled, however (e.g. the [Escape] key is pressed) it’s now our responsibility to revert the cell value using the _revertText value that was pushed when the editor was shown.


Preconditions:
  • DefaultCellStyle.WrapMode is set to DataGridViewTriState.True
  • AutoSizeRowsMode is set to DataGridViewAutoSizeRowsMode.AllCells
  • [Shift][Enter] to add new line
public partial class MainForm : Form
{
    public MainForm() => InitializeComponent();
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        dataGridView.DataSource = Records;
        dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        dataGridView.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
        dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
        dataGridView.EditingControlShowing += (sender, e) =>
        {
            if (e.Control is TextBox textBox)
            {
                Debug.Assert(textBox.Multiline, "Already true, and this is due to WrapMode property setting");
                _revertText = textBox.Text;
                textBox.TextChanged -= localOnTextChanged;
                textBox.PreviewKeyDown -= localPreviewKeyDown;
                textBox.TextChanged += localOnTextChanged;
                textBox.PreviewKeyDown += localPreviewKeyDown;

                void localOnTextChanged(object? sender, EventArgs e)
                {
                    BeginInvoke(() => // Because drawing artifacts have been known to occur otherwise.
                    {
                        dataGridView.CurrentCell.Value = textBox.Text;
                    });
                }
                void localPreviewKeyDown(object? sender, PreviewKeyDownEventArgs e)
                {
                    if(e.KeyData == Keys.Escape)
                    {
                        dataGridView.CurrentCell.Value = _revertText;
                    }
                }
            }
        };
        dataGridView.CellValidating += (sender, e) =>
        {
            Debug.WriteLine(
                $"Ensure that, regardless, only one cell validation happens per commit {dataGridView.CurrentCell?.Value}");
        };
        Records.Add(new Record());
    }
    string _revertText = string.Empty;
    BindingList<Record> Records = new BindingList<Record>();
}
class Record
{
    public string? Description { get; set; }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật