The datagridview is loading very slowly. How can I optimise it?
The datagridview has 4-5 thousand rows.
I have to generate a datagridview dynamically on a few parameters.(Data from database, No. of columns)
I have to generate the datagridview like this vertically from the table (id,name,contact) in database..
Column1
id
name
contact
Now there can be any number of more empty columns beside column1.
Currently I am following this approach.
- First adding all the empty columns.
- Then adding three rows in each for loop iteration one row for each (id,name,contact).
I am fetching the data from the database and passing it as a
List <string[]>
to theGenerateRows
function.private void GenerateColumns(int colLen) { dataGridViewGenerate.Rows.Clear(); dataGridViewGenerate.Columns.Clear(); DataGridViewColumn col0 = new DataGridViewTextBoxColumn(); col0.HeaderText = "Employee No. & Name"; dataGridViewGenerate.Columns.Add(col0); for (int i = 0; i < colLen; i++) { DataGridViewColumn col = new DataGridViewTextBoxColumn { HeaderText = (_sTime.AddDays(i)).Day.ToString(CultureInfo.InvariantCulture) + " " + (_sTime.AddDays(i)).ToString("ddd") }; dataGridViewGenerate.Columns.Add(col); } private void GenerateRows(List<string[]> empList) { int len = empList.Count; for (int a = 0; a < len; a++) { string[] arr = empList[a]; //row 1 var row1 = new DataGridViewRow(); row1.Cells.Add(new DataGridViewTextBoxCell { Value = arr[0] }); dataGridViewGenerate.Rows.Add(row1); //row 2 var row2 = new DataGridViewRow(); row2.Cells.Add(new DataGridViewTextBoxCell { Value = arr[1] }); dataGridViewGenerate.Rows.Add(row2); //row3 var row3 = new DataGridViewRow(); row3.Cells.Add(new DataGridViewTextBoxCell { Value = arr[2] }); dataGridViewGenerate.Rows.Add(row3); } }
I was thinking to make a procedure in sql which will create a table and fill it with the data. Then just assign the datasource to the datagridview.