I copy data from a "DataGridView" to the clipboard, but it contains lots of mysterious tabs that shouldn't be a part of the data, here's a sample of what I mean:

Keywords	Searches
	omaha nebraska apartments 	260
	houses for rent in omaha nebraska 	140
	houses in omaha nebraska 	720

Notice the tabs on the left side of the clipboard data? I don't know where they came from!
When I try to paste that into my OpenOffice "Calc" application (Excel replacement) it is skewed one column to the right, and I'm sure that's unacceptable.

If someone has MS Excel could you try copying & pasting that into a spreadsheet?

Sure it are tabs?
The only thing to find that out is by making a hexdump.

They're horizontal tabs according to my hex editor.

0D 0A 09

09 = horizontal tab

Is there something really wrong with this I'm missing?

dgv.SelectAll();
            try
            {
                // Add the selection to the clipboard.
                Clipboard.SetDataObject(dgv.GetClipboardContent(),true);
                this.toolStripStatusLabel1.Text =
                    "Data has been copied to the Clipboard.";
            }
            catch (System.Runtime.InteropServices.ExternalException)
            {
                MessageBeep(MB_ICONEXCLAMATION);
                this.toolStripStatusLabel1.Text =
                    "The Clipboard could not be accessed. Please try again.";
            }
            dgv.ClearSelection();

It's placing a tab between each column of the DGV.

It's placing a tab between each column of the DGV.

Ehh, how would you recommend I fix it? Is it normal to have a blank column on the far left? I get the feeling if I paste this into Excel it will have a blank column on the left, but I don't have Excel :(

It is normal to have that blank column on the far left, that's where the special stuff goes in a DGV. Yes, you'll have a blank column because you aren't using that column :)

There is no simple way to fix this as you only want to remove the tab from the front of each line (if you removed all the tabs then your pasting into the spreadsheet would no longer align the columns or each line would end up in one cell).

If the data isn't too large, you could read it into a string, use String.Split on the newline characters to get each line, create a new string by combining each of the lines (skipping the first tab) and inserting a newline between each line as you go.

Quick sample here, have not tested and you might have to make modifications:

String clipData = Clipboard.GetText();
String[] lines = clipData.Split(Environment.Newline);
StringBuilder newClipData = new StringBuilder();
foreach (String line in lines) {
    newClipData.Append(line.Substring(line.IndexOf("\t")+1) + Environment.Newline);
}
Clipboard.SetText(newClipData.ToString());

It is normal to have that blank column on the far left, that's where the special stuff goes in a DGV. Yes, you'll have a blank column because you aren't using that column :)

There is no simple way to fix this as you only want to remove the tab from the front of each line (if you removed all the tabs then your pasting into the spreadsheet would no longer align the columns or each line would end up in one cell).

If the data isn't too large, you could read it into a string, use String.Split on the newline characters to get each line, create a new string by combining each of the lines (skipping the first tab) and inserting a newline between each line as you go.

Quick sample here, have not tested and you might have to make modifications:

String clipData = Clipboard.GetText();
String[] lines = clipData.Split(Environment.Newline);
StringBuilder newClipData = new StringBuilder();
foreach (String line in lines) {
    newClipData.Append(line.Substring(line.IndexOf("\t")+1) + Environment.Newline);
}
Clipboard.SetText(newClipData.ToString());

Woohoo! Problem solved, with simple modifications:

private static void DoDataGridClip(DataGridView dgv)
        {
            String clipData = dgv.GetClipboardContent().GetText();
            String[] lines = Regex.Split(clipData, "\r\n");
            //String[] lines = clipData.Split(Environment.NewLine.ToArray<char>());
            StringBuilder newClipData = new StringBuilder();
            foreach (String line in lines)
            {
                newClipData.Append(line.Substring(line.IndexOf("\t") + 1) + Environment.NewLine);
            }
            Clipboard.SetText(newClipData.ToString(), TextDataFormat.Text);
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.