Index: pgadmin/ctl/ctlSQLGrid.cpp
===================================================================
--- pgadmin/ctl/ctlSQLGrid.cpp	(revision 7164)
+++ pgadmin/ctl/ctlSQLGrid.cpp	(working copy)
@@ -112,6 +112,52 @@
     return str;
 }
 
+wxString ctlSQLGrid::GetExportColName(int col1, int col2)
+{
+    wxArrayInt cols;
+    wxString str;
+    int i;
+
+    if (col2 < col1)
+        return str;
+
+    cols.Alloc(col2 - col1 + 1);
+    for (i = col1; i <= col2; i++) 
+	{
+        cols.Add(i);
+    }
+
+    return GetExportColName(cols);
+}
+
+wxString ctlSQLGrid::GetExportColName(wxArrayInt cols)
+{
+    wxString str;
+    unsigned int col;
+
+    if (GetNumberCols() == 0)
+        return str;
+
+    for (col=0 ; col < cols.Count() ; col++)
+    {
+        if (col > 0)
+            str.Append(settings->GetCopyColSeparator());
+
+        wxString text = GetColLabelValue(cols[col]).BeforeFirst('\n');
+
+		bool needQuote  = false;
+		/* Quote everything */
+		needQuote = true;
+
+		if (needQuote)
+            str.Append(settings->GetCopyQuoteChar());
+        str.Append(text);
+        if (needQuote)
+            str.Append(settings->GetCopyQuoteChar());
+    }
+    return str;
+}
+
 int ctlSQLGrid::Copy()
 {
     wxString str;
@@ -121,8 +167,17 @@
     if (GetSelectedRows().GetCount()) 
 	{
         wxArrayInt rows = GetSelectedRows();
+        int max_cols = GetNumberCols();
+        
+        if (rows.GetCount() > 0)
+        {
+            str.Append(GetExportColName(0, max_cols - 1));
+            str.Append(END_OF_LINE);
+        }
 
-        for (i=0 ; i < rows.GetCount() ; i++)
+        // Copy the rows requested, but don't copy the last line (its a 
+        // blank line and not data)
+        for (i=0 ; i < rows.GetCount() && i < GetNumberRows() - 1 ; i++)
         {
             str.Append(GetExportLine(rows.Item(i)));
     
@@ -135,10 +190,16 @@
     else if (GetSelectedCols().GetCount()) 
 	{
         wxArrayInt cols = GetSelectedCols();
-        size_t numRows = GetNumberRows();
+        size_t numRows = GetNumberRows() - 1; 
 
-        for (i=0 ; i < numRows ; i++)
+        if (numRows > 0)
         {
+            str.Append(GetExportColName(cols));
+            str.Append(END_OF_LINE);
+        }
+        
+        for (i=0 ; i < numRows; i++)
+        {
             str.Append(GetExportLine(i, cols));
     
             if (numRows > 1)
@@ -156,7 +217,21 @@
         x2 = GetSelectionBlockBottomRight()[0].GetCol();
         y1 = GetSelectionBlockTopLeft()[0].GetRow();
         y2 = GetSelectionBlockBottomRight()[0].GetRow();
-
+        
+        // If the selection includes all the columns, provide the 
+        // column names along with copied data
+        if ((x2 - x1 + 1) == GetNumberCols())
+        {
+            str.Append(GetExportColName(x1, x2));
+            str.Append(END_OF_LINE);
+        }
+        
+        // Don't copy the last line (its a blank line and not data)
+        if (y2 >= GetNumberRows() - 1)
+        {
+            y2 = GetNumberRows() - 2;
+        }
+        
         for (i = y1; i <= y2; i++) 
 		{
             str.Append(GetExportLine(i, x1, x2));
Index: pgadmin/include/ctl/ctlSQLGrid.h
===================================================================
--- pgadmin/include/ctl/ctlSQLGrid.h	(revision 7164)
+++ pgadmin/include/ctl/ctlSQLGrid.h	(working copy)
@@ -25,6 +25,8 @@
     wxString GetExportLine(int row);
     wxString GetExportLine(int row, wxArrayInt cols);
     wxString GetExportLine(int row, int col1, int col2);
+    wxString GetExportColName(wxArrayInt cols);
+    wxString GetExportColName(int col1, int col2);
     virtual bool IsColText(int col) { return false; }
     int Copy();
 