0

I have implemented data grid view in virtual mode. My data source contains 5k entries. At a time I want to load only 50. Is there a possibility to delete previous row and add a new one on scroll. So that row count remains constant.

Ex.

public void DataGridView1_Scroll(object sender, ScrollEventArgs e)
        {
int startIndex = dataGridView1.FirstDisplayedScrollingRowIndex;
int endIndex = startIndex + 50 + 1;
//set datagrid view count to end index
dataGridView1.RowCount = endIndex;
//remove previous rows
  startIndex = startIndex - 1;

                    while (startIndex != -1)
                    {
                        dataGridView1.Rows.RemoveAt(startIndex);
                        startIndex--;
                    }
}  
Akanksha
  • 135
  • 1
  • 9
  • [Docs](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.virtualmode?view=netframework-4.7.2) say: _"When the VirtualMode property is true, you create a DataGridView with a set number of rows and columns **and then handle the CellValueNeeded event to populate the cells**"_ – stuartd Nov 22 '18 at 10:05
  • I am handling cell value needed and datagridview is loaded properly. But on scroll I am adding extra rows from datasource as initially only 50 were added. This will keep on increasing the rowcount of datagridview, How can I avoid this ? At a time I want only 50 rows in datagridview. – Akanksha Nov 22 '18 at 10:09

0 Answers0