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:

  1. select multiple, full rows @ time.
  2. include unselected rows between selected ones.
  3. 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

  1. my initial thought handling datagridview.keyup event, 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.
  2. create own class inherting datagridview , override processcmdkey:

    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 true after calling stripemptyfromcopy prevents clipboard getting copy. returning false works... gets overridden default copy , don't know occurs.
  3. combine these ideas catch ctrl+c no matter key released first in keyup event 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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -