Tuesday, July 26, 2011

C# Code snippet, paste to DataGridView !

Often times during your journey as a C# programmer it will be necessary to copy and paste data from Excel spreadsheets.  Here's a code snippet to paste data into a DataGridView.

        private void PasteClipboard(DataGridView dgv)
        {
            try
            {
                DataGridViewRow selectedRow;
                /* Find first selected cell's row (or first selected row). */
                if (dgv.SelectedRows.Count > 0)
                    selectedRow = dgv.SelectedRows[0];
                else if (dgv.SelectedCells.Count > 0)
                    selectedRow = dgv.SelectedCells[0].OwningRow;
                else
                    return;
                /* Get clipboard Text */
                string clipText = Clipboard.GetText();
                /* Get Rows ( newline delimited ) */
                string[] rowLines = Regex.Split(clipText, "\r\n");
                foreach (string row in rowLines)
                {
                    /* Get Cell contents ( tab delimited ) */
                    string[] cells = Regex.Split(row, "\t");
                    DataGridViewRow r = new DataGridViewRow();
                    foreach (string sc in cells)
                    {
                        DataGridViewTextBoxCell c = new DataGridViewTextBoxCell();
                        c.Value = sc;
                        r.Cells.Add(c);
                    }
                    dgv.Rows.Insert(selectedRow.Index, r);
                    this.toolStripStatusLabel1.Text = "Data has been pasted.";
                }


            }
            catch (System.ArgumentException ex)
            {
                MessageBeep(MB_ICONEXCLAMATION);
                this.toolStripStatusLabel1.Text = ex.Message;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

19 comments:

  1. I feel dumb, but I've never heard of C#, is it like part of the C++ family and all that great stuff?

    ReplyDelete
  2. It is a C-family language, but it uses the .NET framework and features a high level of abstraction.

    Why don't you experience C# firsthand, download Visual Studio 2010 Express edition from Microsoft's website.

    http://www.microsoft.com/visualstudio/en-us/products/2010-editions

    Once it's installed (god that takes forever), create a new C# windows forms application and you'll get the general C# idea.

    ReplyDelete
  3. This'll definitely save a buttload of time for many peoples. Thanks! Btw, have you ever done LC-3? I'm supposed to be learning that for my class next semester and it seems so different from other programming languages I've tried.

    ReplyDelete
  4. No but it looks cool, it's somewhat similar to assembly it seems.

    http://www.lc3help.com/tutorials/Intro_to_LC-3/

    Wikipedia claims it has a simple instruction set so learning it should be easy. It will have instructions like "MOV" for "move"ing data and "ADD" for adding.

    ReplyDelete
  5. gosh this stuff is confusing!

    ReplyDelete
  6. The gods for murder seemed so content Following!

    ReplyDelete
  7. I'll make sure to show this to a friend who's trying to learn how to program. :)

    ReplyDelete
  8. Trying to work my way into the IT field. I hope to be capable of this kind of thing someday.
    +followed

    ReplyDelete
  9. What's this... I don't trust it if I don't know exactly what it does :P.

    ReplyDelete
  10. I found this useful, thanks broskill.

    ReplyDelete
  11. Or die in the adventure, be my helps, Following!

    ReplyDelete
  12. @Harris, lol it takes the info on the Windows clipboard and pastes it into a .NET DataGridView control. It's a part of a searching/sorting application with a specialized data format.

    ReplyDelete
  13. This is exactly the code I needed. Thanks for doing it so nicely!

    ReplyDelete