DataGrid copy-paste column headings?

  • Thread starter Thread starter PeterZ
  • Start date Start date
P

PeterZ

Hi,

In a running C# app with a datagrid control I select all rows in the
dataGrid using CTRL-A, I then paste into some other app like notepad or Word
but the column headings get left off.

Is there any way of including column headings when copying/pasting from a
running datagrid to notepad? At this stage it looks like I have to write my
own code but would rather not if someone knows how to do it.

Cheers,
PeterZ
 
PeterZ,

Unfortunately, you are going to have to write it yourself. There is
nothing in the datagrid that appears to store the column headings when
copying and pasting.

Hope this helps.
 
Thanks Nicholas.... off coding I will go.

Just realised it will be a bit more difficult than I first imagined as my
datagrid has multiple TableStyles associated with it along with various
ColumnStyles. So getting the underlying DataTable column names won't be
enough, I will have to match them up against defined TableStyles to find out
what the column alias displayed on the dataGrid really is.

Amazing how something so simple can turn out to be an utter pain in the
tushka!

Cheers,
PeterZ


Nicholas Paldino said:
PeterZ,

Unfortunately, you are going to have to write it yourself. There is
nothing in the datagrid that appears to store the column headings when
copying and pasting.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

PeterZ said:
Hi,

In a running C# app with a datagrid control I select all rows in the
dataGrid using CTRL-A, I then paste into some other app like notepad or
Word but the column headings get left off.

Is there any way of including column headings when copying/pasting from a
running datagrid to notepad? At this stage it looks like I have to write
my own code but would rather not if someone knows how to do it.

Cheers,
PeterZ
 
Here's my effort in case anyone is interested.

Just call the CopyToClipboard() function passing it the datagrid as a
parameter.


-------------------------------------------
public static void CopyToClipboard(DataGrid dg)
{
// This method copies the contents of a datagrid to the clipboard
/// including the column headings.

int i, j=0, iColCount;
string sColumns, s = "";

DataTable dt = (DataTable)dg.DataSource;

// Retrieve datagrid column headings.
sColumns = CopyToClipboard_GetColumnNames(dg);

// Write contents of datagrid to string variable.
iColCount = dt.Columns.Count;
for (i=0; i<=dt.Rows.Count-1; i++)
{
if (dg.IsSelected(i)) // check if row is selected.
{
try
{
for (j=0; j<=iColCount-1; j++)
{
s += dg[i,j].ToString();
if (j < iColCount-1)
s += "\t";
}
}
catch
{
// Because we might be using TableStyles which might not display
all columns we
// have to catch if we go beyond the index range of columns
displayed in the datagrid.
iColCount = j;
s = s.Substring(0, s.Length-1); // remove trailing tab.
}
s += "\r\n";
}
}

if (s == "")
Clipboard.SetDataObject("");
else
Clipboard.SetDataObject(sColumns + s);
}


private static string CopyToClipboard_GetColumnNames(DataGrid dg)
{
// Helper function for CopyToClipboard() method.

// Returns a tab seperated list of column names used on the datagrid.
This function
// will check any TableStyles used by the dataGrid for alias column
names.

string s = "";
DataTable dt = ((DataTable)dg.DataSource);
string sTabName = dt.TableName;

// Check if the datagrid has a TableStyle with a matching table
MappingName.
if (dg.TableStyles.Contains(sTabName))
{
// Use the columnn names as defined in the TableStyle.
DataGridTableStyle ts = dg.TableStyles[sTabName];
foreach (DataGridColumnStyle cs in ts.GridColumnStyles)
{
if (cs.Width > 0) // ensure the column is visible.
s += cs.HeaderText + "\t";
}
}
else
{
// If no matching TableStyle is defined then use column names from
underlying DataTable.
foreach (DataColumn dc in dt.Columns)
s += dc.ColumnName + "\t";
}
s = s.Substring(0, s.Length-1); // remove trailing tab.
return s + "\r\n";
}
 
Back
Top