PC Review


Reply
Thread Tools Rate Thread

DataGridView.GetClipboardContent does not call ParseFormattedValue

 
 
jonpb
Guest
Posts: n/a
 
      5th Nov 2009
Hi,
The default implelmentation of DataGridView.GetClipboardContent does not
call DataGridViewCell.ParseFormattedValue. Does anyone know how to force
it do this, or how to implement GetClipboardContent so that it does. I
am not clear on how, exactly to build a DataObject to put on the Clipboard.

Thanks
 
Reply With Quote
 
 
 
 
jonpb
Guest
Posts: n/a
 
      6th Nov 2009
jonpb wrote:
> The default implelmentation of DataGridView.GetClipboardContent does not
> call DataGridViewCell.ParseFormattedValue. Does anyone know how to force
> it do this, or how to implement GetClipboardContent so that it does.


For anyone interested, this is my solution. Warning: this code has not
been thoroughly tested, but the general idea is there:

public override DataObject GetClipboardContent()
{
DataObject data = new DataObject();

if (this.SelectedCells.Count == 0) return data;

Dictionary<int, Dictionary<int, string>> tabs = new Dictionary<int,
Dictionary<int, string>>();
Dictionary<int, Dictionary<int, string>> csvs = new Dictionary<int,
Dictionary<int, string>>();
Dictionary<int, string> rt, rc;
StringBuilder tab = new StringBuilder();
StringBuilder csv = new StringBuilder();
foreach (DataGridViewCell cell in this.SelectedCells)
{
if (!tabs.TryGetValue(cell.RowIndex, out rt))
{
rt = new Dictionary<int, string>();
tabs.Add(cell.RowIndex, rt);
rc = new Dictionary<int, string>();
csvs.Add(cell.RowIndex, rc);
}
else
rc = csvs[cell.RowIndex];

rt.Add(cell.ColumnIndex, cell.Value.ToString());
rc.Add(cell.ColumnIndex, cell.Value.ToString());
}

List<int> cols = new List<int>();
List<int> rows = new List<int>();
foreach (int i in tabs.Keys)
rows.Add(i);
rows.Sort();

string s;
foreach (int i in rows)
{
cols.Clear();
foreach (int k in tabs[i].Keys)
cols.Add(k);
cols.Sort();

foreach (int k in cols)
{
tab.AppendFormat("{0}\t", tabs[i][k]);
csv.AppendFormat("{0},", tabs[i][k]);
}

tab.Remove(tab.Length - 1, 1);
tab.AppendLine();
csv.Remove(csv.Length - 1, 1);
csv.AppendLine();
}

data.SetData(DataFormats.CommaSeparatedValue, csv.ToString());
data.SetData(DataFormats.UnicodeText, tab.ToString());
data.SetData(DataFormats.Text, Encoding.ASCII.GetBytes(tab.ToString()));

DataObject html = base.GetClipboardContent();
data.SetData(DataFormats.Html, html.GetData(DataFormats.Html));

return data;
}
 
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
Does DataGridView.Dispose Call DataGridViewRow.Dispose? eBob.com Microsoft VB .NET 13 31st Jul 2009 02:08 PM
[.NET 2 / DataGridView] What method to call for refreshing display when DataSource property was updated newsgroup.poster@gmail.com Microsoft Dot NET Framework Forms 3 4th Nov 2006 12:43 AM
DataGridView.Refresh() hangs on cross-thread call David Cartwright Microsoft Dot NET 3 26th Oct 2006 10:04 PM
System.InvalidOperationException will be thrown if I call the method DataGridView.Rows.Add, How to resolve? Ryan Microsoft C# .NET 2 16th Oct 2006 05:41 PM
Refreshing datagridview contents after a failed delete in bound datagridview Bob Microsoft VB .NET 0 5th Jan 2006 06:34 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:47 AM.