简体   繁体   中英

How can I fill datagridview faster c#

I have the following code:

 private void copyCloneRowFromdgvCustomMainTodgvCustomSingleExtraction(int[] vect)
    {
        dgvCustomSingleExtraction.Rows.Clear();
        DataGridViewRow row = new DataGridViewRow();
        int i = 0;
        dgvCustomSingleExtraction.ColumnCount = dgvCustomMain.ColumnCount;
        for (; i < dgvCustomMain.ColumnCount; i++)
            dgvCustomSingleExtraction.Columns[i].HeaderCell.Value = dgvCustomMain.Columns[i].HeaderCell.Value;

        for (i = 0; i < vect.Length; i++)
        {
            int intColIndex = 0;
            row = (DataGridViewRow)dgvCustomMain.Rows[vect[i]].Clone();
            foreach (DataGridViewCell cell in dgvCustomMain.Rows[vect[i]].Cells)
            {
                row.Cells[intColIndex].Value = cell.Value;
                intColIndex++;
            }  

            dgvCustomSingleExtraction.Rows.Add(row);
        }
    }

With this method I want create a new DataGridView with random rows that are in dgvCustomMain . So I have used int[] vect that it's an array with random number(from 0 to dgvCustomMain.Count - 1 ). Now if the array is small for example 1500 elements it takes 188ms , if the array is bigger(for example 15000 elements) it takes about 1800ms and if it's more bigger like 150000 elements its take about 20s. Now my question is: how can I fill faster the datagridview?

EDIT

I take a look here but I don't understand how to implements it. Someone could help me?

You can use Virtual mode to improve the efficiency of DataGridView

See here: link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM