2

I have a simple data grid view and I am copying its content as below:

enter image description here

When i paste this to note pad, it appear as below:

enter image description here

How can i remove the blank line between 1 Jake and 3 Tom?

I am using this code to copy:

 private void copySelectedToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (this.dataGridView1.GetCellCount(DataGridViewElementStates.Selected) > 0)
        {
            try
            {

                Clipboard.SetDataObject(
                    this.dataGridView1.GetClipboardContent());

            }
            catch (System.Runtime.InteropServices.ExternalException)
            {
                // "The Clipboard could not be accessed. Please try again.";
            }
        }
    }

Is there is any better way to copy only selected? Please Help.

4

2 回答 2

3

我解决了它如下:

if (this.dataGridView1.SelectedRows.Count > 0)
{
    StringBuilder ClipboardBuillder = new StringBuilder();
    foreach (DataGridViewRow Row in dataGridView1.SelectedRows)
    {
        foreach (DataGridViewColumn Column in dataGridView1.Columns)
        {
           ClipboardBuillder.Append(Row.Cells[Column.Index].FormattedValue.ToString() + " ");
        }
        ClipboardBuillder.AppendLine();
    }

    Clipboard.SetText(ClipboardBuillder.ToString());
}
于 2013-09-10T08:21:22.523 回答
0

您可以遍历所有选定的行:

string clipboard = "";
foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)
{
    foreach (DataGridViewColumn dgvc in dg_autoTestStatus.Columns)
        clipboard += dataGridView1.Rows[dgvr.Index].Cells[dgvc.Index].FormattedValue.ToString() + " ";
    clipboard += "\n"
}
Clipboard.SetText(clipboard);
于 2013-09-10T08:16:33.460 回答