PC Review


Reply
Thread Tools Rate Thread

DataGrid copy-paste column headings?

 
 
PeterZ
Guest
Posts: n/a
 
      10th Jan 2005
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



 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      10th Jan 2005
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 Removed)

"PeterZ" <_(E-Mail Removed)> wrote in message
news:crsion$3a1$(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
PeterZ
Guest
Posts: n/a
 
      10th Jan 2005
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 [.NET/C# MVP]" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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 Removed)
>
> "PeterZ" <_(E-Mail Removed)> wrote in message
> news:crsion$3a1$(E-Mail Removed)...
>> 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
>>
>>
>>

>
>



 
Reply With Quote
 
PeterZ
Guest
Posts: n/a
 
      11th Jan 2005
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";
}




"PeterZ" <_(E-Mail Removed)> wrote in message
news:crsion$3a1$(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy and paste outline view headings into new document =?Utf-8?B?amttYXI1?= Microsoft Word Document Management 3 14th Jul 2010 09:33 PM
How do I repeat column headings on all of my sheets w/o cut/paste =?Utf-8?B?TW9tb2YzYm95cw==?= Microsoft Excel Misc 1 16th Sep 2005 04:45 AM
Changing DataGrid Column headings Mac via DotNetMonster.com Microsoft VB .NET 4 23rd May 2005 09:07 AM
Copy/Paste from Access to Excel without Headings Milan Microsoft Access Forms 1 3rd Jul 2004 07:39 PM
Copy/Paste from Access to Excel without Headings Milan Microsoft Access VBA Modules 0 29th Jun 2004 04:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:59 AM.