c# datagridview vertical scroll too slow in WrapMode + AutoSizeRowsMode RRS feed

  • Question

  • I am displaying multi-line text in DataGridView Cell.
    To show multiline text I need to set WrapMode = true and AutoSizeRowsMode = allcells.
    If AutoSizeRowsMode = none, I can't use it because the entire contents are not displayed and only a portion is displayed.

    However, setting wrapmode = true and AutoSizeRowsMode =allcells is too slow to scroll vertically using the mouse wheel.
    I want to scroll vertically quickly and naturally.

    May I ask for some advice?

    using System;
    using System.Data;
    using System.Windows.Forms;
    namespace test_dataGridView
        public partial class Form1 : Form
            private DataTable tb;
            public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
            private const int WM_SETREDRAW = 0x000B;
            public Form1()
            private void Form1_Load(object sender, EventArgs e)
                tb = new DataTable("itemTable");
                DataColumn[] cols ={
                                      new DataColumn("No",typeof(String)),
                                      new DataColumn("Contents",typeof(String))
                string contents = String.Empty;
                for (int i = 1; i <= 50; i++)
                    contents += "Why Vertical Scroll lagging in wrapmode true? ";
                for (int i = 1; i <= 200; i++)
                    DataRow row = tb.NewRow();
                    row["No"] = i.ToString();
                    row["Contents"] = "★★★★★" +  contents;
                dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
                dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
                dataGridView1.ColumnHeadersHeight = 30;
                dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dataGridView1.RowHeadersVisible = false;
                dataGridView1.AllowUserToAddRows = false;
                dataGridView1.AllowUserToDeleteRows = false;
                dataGridView1.AllowUserToResizeRows = false;
                dataGridView1.AllowUserToResizeColumns = false;
                dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
                SendMessage(dataGridView1.Handle, WM_SETREDRAW, false, 0);
                dataGridView1.DataSource = tb;
                dataGridView1.Columns["Contents"].Width = 650; //Contents
                dataGridView1.Columns["Contents"].DefaultCellStyle.WrapMode = DataGridViewTriState.True; // *****
                dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None; //*****
                dataGridView1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells); //****
                SendMessage(dataGridView1.Handle, WM_SETREDRAW, true, 0);
        public class DataGridViewDoubleBuffered : DataGridView
            public DataGridViewDoubleBuffered()
                DoubleBuffered = true;
    Monday, August 3, 2020 7:29 AM

All replies

  • Hi greinke21,
    You can autosize the rows by using the following code

    dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;

    instead of DataGridViewAutoSizeRowsMode.AllCells.
    Here is a similar thread you can refer to.
    Best Regards,
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 3, 2020 8:42 AM
  • Thanks for the answer
    dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
    If I set this, only the screen shown is multiline text, and scrolling down does not adjust the row height, so the multiline text is not visible.
    I tried connecting to the datagridview scroll event according to the answer link, but the scrolling behavior is abnormal.

    • Edited by greinke21 Monday, August 3, 2020 9:26 AM
    Monday, August 3, 2020 9:21 AM
  • Hello,

    An alternate idea is to use a TextBox bound to the column currently used in the DataGridView e.g.

    contentTextBox.DataBindings.Add("Text", tb, "Contents");

    Also, on a side note I recommend using a BindingSource as this permits getting to your data without accessing the actual DataGridView e.g..

    private BindingSource _bindingSource = new BindingSource();


    _bindingSource.DataSource = tb;


    Later we can use

    var currentRow = ((DataRowView) _bindingSource.Current).Row;

    To a column value

    var contents = currentRow.Field<string>("Contents");

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Monday, August 3, 2020 1:01 PM