Reputation: 57
Currently, I'm planning to add ~500K List to datagridview.
Class has multiple Fields, Each List has 13 fields. but i bind only 7 fields for now.
Problem is,it seems like adding takes too much time. like 5sec for 15K which is awful.
Is there any ways to optimze this? or should i throw away datagridview and consider some other views?
private void UpdateDataGrid()
{
this.dataGridView1.Rows.Clear();
for (int i = 0; i < gVar.gTagCount; i++)
{
this.dataGridView1.Rows.Add(new object[]
{
gVar.Tags[i].TagCount,
gVar.Tags[i].Name,
gVar.Tags[i].Score.Story,
gVar.Tags[i].Score.Drawing,
gVar.Tags[i].Score.Drawing,
gVar.Tags[i].Score.Memetic,
gVar.Tags[i].DupeCount
});
}
}
Upvotes: 0
Views: 451
Reputation: 1197
According to what we discussed my approach would be this:
First make a new Class, I would call it TagsMin this class should contain the 7 things you want in your datagridview.
Secondly i would populate that a list of that class like this (you need to complete it with what you want):
var tagList = gVar.Tags.Select(x => new TagsMin() { TagCount = x.TagCount, Name = x.Name... }).ToList()
And the last step, would be to bind it to the datagridview:
dataGridView1.DataSource = tagList;
Upvotes: 2
Reputation: 1186
Can you try to avoid the loop and directly bind the list with the standard way:
dataGridView1.DataSource = null;
dataGridView1.DataSource = gVar.Tags;
Upvotes: 0
Reputation: 404
Consider using paging so that you are not loading all of the data at once. The answer to the question linked to below provides an example.
How can we do pagination in datagridview in winform
Upvotes: 2