Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7907097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:16:23+00:00 2026-06-03T11:16:23+00:00

I have a DataGridView in a .Net application (V4 C# VS2010) & want to

  • 0

I have a DataGridView in a .Net application (V4 C# VS2010) & want to copy all the data to the clipboard on the click of a button. No problem –

private void copyToClipboard()
{
    dataGridView1.SelectAll();
    DataObject dataObj = dataGridView1.GetClipboardContent();
    if (dataObj != null)
        Clipboard.SetDataObject(dataObj);
}

Problem is that the user might already have some cells, rows etc selected on the DataGrid & I don’t really want to change that selection. The above obviously selects everything. I could dataGridView1.ClearSelection(); at the end which is marginally better but still doesn’t achieve what’s required.

I can save the selected cells:

var mySelectedCells = dataGridView1.SelectedCells;

but how do I get those selected cells reselected on the DataGrid after the copy? Is there an easy way to get the selected cells collection back into the DataGrid? Perhaps there is a better way to get the whole grid copied to the clipboard in the first place without affecting presently selected cells?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:16:24+00:00Added an answer on June 3, 2026 at 11:16 am

    I suppose if you just wanted to represent the contents of the cells as text and copy them to the clipboard, tab-delimited, you could do something like:

        var newline = System.Environment.NewLine;
        var tab = "\t";
        var clipboard_string = "";
    
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
             for (int i=0; i < row.Cells.Count; i++)
             {
                  if(i == (row.Cells.Count - 1))
                       clipboard_string += row.Cells[i].Value + newline;
                  else
                       clipboard_string += row.Cells[i].Value + tab;
             }
        }
    
        Clipboard.SetText(clipboard_string);
    

    The output seems pretty similar to that of the GetClipboardContent(), but be careful for any DataGridViewImageColumns or any type that isn’t implicitly a string.

    Edit: Anthony is correct, use StringBuilder to avoid allocating a new string for every concatenation. The new code:

        var newline = System.Environment.NewLine;
        var tab = "\t";
        var clipboard_string = new StringBuilder();
    
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            for (int i = 0; i < row.Cells.Count; i++)
            {
                if (i == (row.Cells.Count - 1))
                    clipboard_string.Append(row.Cells[i].Value + newline);
                else
                    clipboard_string.Append(row.Cells[i].Value + tab);
            }
        }
    
        Clipboard.SetText(clipboard_string.ToString());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have encountered a rather nasty problem with the DataGridView control (Windows.Forms, .NET Framework
I am using vb.net. I have a datagridview to show data. Since the datagridview
For a VB.Net application needing to output the data to clipboard, with formatting, I
I have a datagridview in a c# .net WinForms application that captures order detail
I have a DataGridView form in my .NET application and I'd like to be
I have following code for 3 DataGridView Controls in my VB.NET winform application. How
I'm working on a desktop application in C# (.NET 4.0). I have a datagridview
In a VB.NET Winforms application, I have a form that contains both a datagridview
VB.NET 4.0 framework Windows Forms Application. So I have a DataGridView that I have
I'm writing a small application in C# (.NET 4.0). I have a datagridview, each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.