Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Help With dataGridView Copy and Paste Feature

Help With dataGridView Copy and Paste Feature

Scheduled Pinned Locked Moved C#
helpquestiondatabase
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    That Asian Guy
    wrote on last edited by
    #1

    I recently made a program for organizing marks and realized that it is slow and repetitive to type the same teacher's name 20 times for students in the same class. I chose to add a little copy and paste feature when the user presses ctrl+v and the active cell becomes the contents of the clipboard. Here is the snippet:

    else if (e.Control && e.KeyCode == Keys.V)
    {
    if (Clipboard.ContainsText() & dataGridView1.CurrentCell.OwningColumn.Index == 4)
    {
    dataGridView1.CurrentCell.Value = Clipboard.GetText();
    }

                // If its the last row, add another row
                if (dataGridView1.CurrentCell.OwningRow.Index == nUD\_counter)
                {
                    dataGridView1.Rows.Add();
                }
            }
    

    What I'm having problems with is the last part, where I add another row (Microsoft made it so it would automatically add a row if the cell is edited by the user, but not programatically. The problem is that when I add the new row, it places it above the row which I just pasted info into such that:

    col1 col2
    a b
    <-blank line where I press ctrl+v->

    It becomes

    col1 col2
    a b
    <-blank line where I press ctrl+v->
    hi

    How can I fix this? Also feel free to make suggestions on my code. Thanks in advance.

    modified on Saturday, November 1, 2008 5:23 PM

    T 1 Reply Last reply
    0
    • T That Asian Guy

      I recently made a program for organizing marks and realized that it is slow and repetitive to type the same teacher's name 20 times for students in the same class. I chose to add a little copy and paste feature when the user presses ctrl+v and the active cell becomes the contents of the clipboard. Here is the snippet:

      else if (e.Control && e.KeyCode == Keys.V)
      {
      if (Clipboard.ContainsText() & dataGridView1.CurrentCell.OwningColumn.Index == 4)
      {
      dataGridView1.CurrentCell.Value = Clipboard.GetText();
      }

                  // If its the last row, add another row
                  if (dataGridView1.CurrentCell.OwningRow.Index == nUD\_counter)
                  {
                      dataGridView1.Rows.Add();
                  }
              }
      

      What I'm having problems with is the last part, where I add another row (Microsoft made it so it would automatically add a row if the cell is edited by the user, but not programatically. The problem is that when I add the new row, it places it above the row which I just pasted info into such that:

      col1 col2
      a b
      <-blank line where I press ctrl+v->

      It becomes

      col1 col2
      a b
      <-blank line where I press ctrl+v->
      hi

      How can I fix this? Also feel free to make suggestions on my code. Thanks in advance.

      modified on Saturday, November 1, 2008 5:23 PM

      T Offline
      T Offline
      That Asian Guy
      wrote on last edited by
      #2

      I messed around with it a bit and ended up using SendKeys. I know this isn't the best method, but its the only one I tried so far that works. Basically,

      try
      {
      dataGridView1.BeginEdit(true);
      String tempText = Clipboard.GetText();
      tempText = tempText.Replace("{", "{{}");
      tempText = tempText.Replace("}", "{}}");
      tempText = tempText.Replace("{{{}}", "{{}");
      tempText = tempText.Replace("(", "{(}");
      tempText = tempText.Replace(")", "{)}");
      tempText = tempText.Replace("+", "{+}");
      tempText = tempText.Replace("^", "{^}");
      tempText = tempText.Replace("%", "{%}");
      tempText = tempText.Replace("~", "{~}");
      SendKeys.Send(tempText);
      SendKeys.Send("{DOWN}");
      }
      catch (Exception ex)
      {
      MessageBox.Show(ex.Message + "\n\nThe previous value was restored.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
      dataGridView1.CurrentCell.Value = previousValue;
      }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups