c# - Adding items in ListView too slow -
i have listview , add items one-by-one loop
listview has checkboxes
in loop decide whether checkbox should checked or not
problem if many checkboxes should checked adding items slow
here code:
for (int = 0; < dt.rows.count; i++) { datarow drow = dt.rows[i]; // row have not been deleted if (drow.rowstate != datarowstate.deleted && int.parse(drow["season"].tostring()) != 0) { listviewitem lvi = new listviewitem(drow["episode_name"].tostring()); lvi.subitems.add(drow["first_aired"].tostring()); lvi.subitems.add(drow["episode"].tostring()); lvi.subitems.add(drow["season"].tostring()); lvi.subitems.add(drow["rating"].tostring()); lvi.subitems.add(drow["episode_id"].tostring()); if (bool.parse(drow["watched"].tostring())) { lvi.checked = true; //this problem, when remove it, adding fast } else { lvi.checked = false; } episodeslist.items.add(lvi); } }
how can make faster?
on listview, call .beginupdate()
while you're loading in results. after you're finished, call .endupdate()
. should speed it's not trying process , draw @ same time.
// wait draw episodeslist.beginupdate(); // add items // draw episodeslist.endupdate();
edit
as justin suggested, addrange() some, won't eliminate problem. see: how speed adding items listview?
edit #2
since event handlers causing issue, can work around them removing handlers during load , re-adding them after. or, can use global boolean _isloading
set before load , after , can check _isloading
in handlers don't make trip db. removing/readding handler cleaner solution, though.
Comments
Post a Comment