c# - visually select datagridview cell -
i trying use datagridview "tag" mesh. have number of objects want set , edit "tags" for. each cell in datagridview has string tag in it, datagridview multi-select, user can select whole lot of tags.
it works great setting tags...
however, want able edit them. so, when load datagridview, want programatically select cells corresponding existing tags.
code quite straight forward:
public frmsavequery(string name, string description, string taglist, list<tagtype> alltags) { initializecomponent(); taglist = alltags; cancelled = true; txtqueryname.text = name; txtdescription.text = description; string[] tags = taglist.split(new string[] {"|"}, stringsplitoptions.removeemptyentries); foreach (datagridviewrow row in tagselector.rows) { foreach (datagridviewcell cell in row.cells) { if (tags.contains(cell.value.tostring().toupper())) { cell.selected = true; } else { cell.selected = false; } } } foreach (datagridviewrow row in tagselector.rows) { foreach (datagridviewcell cell in row.cells) { if (cell.selected) debug.writeline (cell.value.tostring()); } } }
the debug verifies cells "selected". however, on actual datagridview, don't visually selected cells (i.e. not highlighted blue).
any idea how make them visually selected?
before controls displayed cannot set focus them.
event handler of shown
event place this. event raised once when form displayed first time (msdn form.shown event).
you need save tags
values taglist
in variable later using in shown
eventhandler
private string[] _tags; public frmsavequery(string name, string description, string taglist, list<tagtype> alltags) { initializecomponent(); taglist = alltags; cancelled = true; txtqueryname.text = name; txtdescription.text = description; //save tags in class variable _tags = taglist.split(new string[] {"|"}, stringsplitoptions.removeemptyentries); //wiring handler event this.shown += frmsavequery_shown; } public void frmsavequery_shown(object sender, eventargs e) { if (_tags == null || _tags.length == 0) return; foreach (datagridviewrow row in tagselector.rows) { foreach (datagridviewcell cell in row.cells) { if (tags.contains(cell.value.tostring().toupper())) { cell.selected = true; } else { cell.selected = false; } } } foreach (datagridviewrow row in tagselector.rows) { foreach (datagridviewcell cell in row.cells) { if (cell.selected) debug.writeline (cell.value.tostring()); } } }
Comments
Post a Comment