Reputation: 361
I am trying to assign large data ( about 230,000 row x 16 column ) from dataset to datagridview in a winform and it takes a very long time to be loaded in the datagridview.
After searching the web about this problem I found two solution to speed up the loading process, one using the virtualmode for the datagridview and the other solution by directly assigning the dataset to the datagridview datasource.
Both solution are working but still take a long time, as the 1st solution takes about 15 mins to be completed while the 2nd solution takes about 30 mins.
So I listed below the 2 solutions in order to check that I didn't miss something or maybe there is another solution for this issue.
1st solution
public Form1()
{
InitializeComponent();
this.dataGridView1.Dock = DockStyle.Fill;
this.Controls.Add(this.dataGridView1);
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.VirtualMode = true;
this.dataGridView1.CellValueNeeded += new
DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
DataSet DSg = ACC_Data.Get_DT(File_Path.Text.ToString());
for (int i = 0; i < DSg.Tables[0].Columns.Count; i++)
{
this.dataGridView1.Columns.Add(DSg.Tables[0].Columns[i].ColumnName, DSg.Tables[0].Columns[i].Caption);
}
this.dataGridView1.RowCount = DSg.Tables[0].Rows.Count;
}
private void dataGridView1_CellValueNeeded(object sender,
System.Windows.Forms.DataGridViewCellValueEventArgs e)
{
if (e.RowIndex == this.dataGridView1.RowCount - 2 && e.ColumnIndex == 0)
{
return;
}
e.Value = DSg.Tables[0].Rows[e.RowIndex][e.ColumnIndex];
}
2nd Solution
public Form1()
{
InitializeComponent();
this.dataGridView1.Dock = DockStyle.Fill;
this.Controls.Add(this.dataGridView1);
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
DataSet DSg = ACC_Data.Get_DT(File_Path.Text.ToString());
this.dataGridView1.DataSource = DSg.Tables[0];
}
Thanks in advance.
Upvotes: 0
Views: 804
Reputation: 361
Thanks to @ASh, I just add a dummy table and bind it to the datagridview as shown in the code below, and it loaded the data in about 20 sec.
private void Form1_Load(object sender, EventArgs e)
{
DataSet DSg = ACC_Data.Get_DT(File_Path.Text.ToString());
var t = new DataTable();
for (int c = 0; c < DSg.Tables[0].Columns.Count; c++) t.Columns.Add();
for (int r = 0; r < DSg.Tables[0].Rows.Count; r++)
{
string[] Dr = new string[DSg.Tables[0].Columns.Count];
int i = 0;
foreach (DataColumn C in DSg.Tables[0].Columns)
{
Dr[i] = DSg.Tables[0].Rows[r][C].ToString();
i++;
}
var row = t.Rows.Add(Dr);
}
dataGridView1.DataSource = t;
}
Upvotes: 0
Reputation: 23797
VirtualMode is fast itself. Here I have sample video showing VirtualMode with 9+ million rows (Northwind Customers table replicated N times):
Initial load of form takes around 8 seconds, and from there on it is fast.
IMHO, it is the dataset itself that you are loading slow. You shouldn't load the whole data at once, and that is the point of using approaches like VirtualMode. Unfortunately DataSet is born as a slow technology. IOW you should overcome the slowness in this line:
DataSet DSg = ACC_Data.Get_DT(File_Path.Text.ToString());
If you share ACC_Data.Get_DT() code then we might have suggestions on loading that faster (15 mins is really slow even for direct loading of a DataSet with 230K rows).
PS: I published the code for that Video here:
Upvotes: 0
Reputation: 138
i don't think anyone scrolls and find a row from this big size data.. you should just get top 100 or 500 values assigned to datagrid and give a search option for rest of others using text boxes or whatever.
Upvotes: 1