c# - Copy only selected rows without not-selected rows between in RowHeaderSelect mode -
i'm looking possibility in c# in windows forms application able to:
- select multiple, full rows @ time.
- include unselected rows between selected ones.
- then copy them clipboard without empty spaces (which left unselected rows).
the datagridviewselectionmode.fullrowselect doesn't cut need able select independent cells. need enable same behaviour in row copying datagridviewselectionmode.fullrowselect has datagridviewselectionmode.rowheaderselect mode. possible do?
thanks in advanced.
first, manually removing empty lines default copy results. call following method so:
private void stripemptyfromcopy() { string[] separated = clipboard.gettext().split('\n').where(s => !string.isnullorwhitespace(s)).toarray(); string copy = string.join("\n", separated); if (!string.isnullorempty(copy)) { clipboard.settext(copy); } } solutions , cons
my initial thought handling
datagridview.keyupevent, checking user input ctrl+c:private void datagridview1_keyup(object sender, keyeventargs e) { if (e.keycode == keys.c && (control.modifiers & keys.control) == keys.control) { stripemptyfromcopy(); e.handled = true; } }- works fine if c released first. if ctrl released first, empty rows still in clipboard.
create own class inherting
datagridview, overrideprocesscmdkey:public class copydatagridview : datagridview { protected override bool processcmdkey(ref message msg, keys keydata) { if (keydata == (keys.control | keys.c)) { stripemptyfromcopy(); return false; } return base.processcmdkey(ref msg, keydata); } }- returning
trueafter callingstripemptyfromcopyprevents clipboard getting copy. returningfalseworks... gets overridden default copy , don't know occurs.
- returning
combine these ideas catch ctrl+c no matter key released first in
keyupevent handler:public class copydatagridview : datagridview { public bool copying { get; set; } protected override bool processcmdkey(ref message msg, keys keydata) { copying = keydata == (keys.control | keys.c); return base.processcmdkey(ref msg, keydata); } } // , in form: copydatagridview1.keyup += copydatagridview1_keyup; private void copydatagridview1_keyup(object sender, keyeventargs e) { if (copydatagridview1.copying) { stripemptyfromcopy(); copydatagridview1.copying = false; e.handled = true; } }- it's more work, gives consistent results vs. option 1.
Comments
Post a Comment