Click here to Skip to main content
15,911,896 members
Home / Discussions / C#
   

C#

 
GeneralRe: Child Control Painting problem Pin
Jon_Slaughter8-Jul-04 13:26
Jon_Slaughter8-Jul-04 13:26 
GeneralPostMessage Pin
kobybr8-Jul-04 11:38
kobybr8-Jul-04 11:38 
GeneralRe: PostMessage Pin
Serge Lobko-Lobanovsky9-Jul-04 3:44
Serge Lobko-Lobanovsky9-Jul-04 3:44 
GeneralRe: PostMessage Pin
kobybr9-Jul-04 8:56
kobybr9-Jul-04 8:56 
Questiondsoframer disables plugins?? Pin
Asad Hussain8-Jul-04 11:00
Asad Hussain8-Jul-04 11:00 
GeneralCopy from Excel and paste on DataGrid Pin
Ruchi Gupta8-Jul-04 10:54
Ruchi Gupta8-Jul-04 10:54 
GeneralRe: Copy from Excel and paste on DataGrid Pin
Nick Parker8-Jul-04 11:30
protectorNick Parker8-Jul-04 11:30 
GeneralRe: Copy from Excel and paste on DataGrid Pin
Heath Stewart8-Jul-04 12:10
protectorHeath Stewart8-Jul-04 12:10 
When you copy data in Excel, it uses several clipboard formats to store data (actually, quite a few more than "several"). Several of these will work, but your best bet is to use the "Csv" clipboard format):
IDataObject data = Clipboard.GetDataObject();
if (data != null)
{
  string csv = data.GetData("Csv") as string;
  if (csv != null)
  {
    using (StringReader reader = new StringReader(csv))
    {
      string line = null;
      while ((line = reader.ReadLine()) != null)
        // Continue processing...
    }
  }
}
Where I've placed the comment you can either take the cheap way out and use line.Split(',').Trim('\"'), or parse it correctly by using a token reader. While there's no standard CSV format, the agreed-upon convention is to quote a field when the field value contains a comma (since the file is comma-delimited). So, when you encounter a quote set a flag (or increment a variable, whatever). When you encounter a comma, if the flag is set, then don't split the line. When you enounter the next quote, reset the flag. As long as that flag isn't set, you can split the field at the comma when you hit one.

There's other formats you can use as well. Excel 2003 copies the new standard Excel XML format to the clipboard, as well as many other formats (both text and binary formats).

You can use Mike Dunn's ClipSpy[^] to view the different formats, as well as the data those formats contain.

The real trick here is that you have to have some way to paste into the DataGrid. Once one of the cells is in edit mode, the DataGrid receives little to no notification; the control being hosted in the current cell is receiving notifications. You could either use a paste button or jump through hoops to support Ctrl+V (or the mouse) to paste into the DataGrid.

From there, it's as simple as the getting the data source (taking both DataSource and DataMember into account) and adding a new row/object for each line and, if applicable, each field in that row for each imported field from the CSV clipboard format.

And easy way is to use the CurrencyManager like so:
CurrencyManager cm = (CurrencyManager)dataGrid1.BindingContext[
  dataGrid1.DataSource, dataGrid1.DataMember];
if (cm != null)
{
  IList list = cm.List;
  if (list is DataView)
  {
    DataTable dt = ((DataView)list).Table;
    foreach (/* line in the parsed csv file */)
      DataRow row = dt.LoadDataRow(line.Split(','), false);
  }
  // Other conditions...
}


 

Microsoft MVP, Visual C#
My Articles
GeneralRepainting window. Pin
Nick Z.8-Jul-04 9:59
Nick Z.8-Jul-04 9:59 
GeneralRe: Repainting window. Pin
Heath Stewart8-Jul-04 11:24
protectorHeath Stewart8-Jul-04 11:24 
GeneralRe: Repainting window. Pin
Nick Z.8-Jul-04 15:24
Nick Z.8-Jul-04 15:24 
GeneralRe: Repainting window. Pin
Heath Stewart8-Jul-04 16:34
protectorHeath Stewart8-Jul-04 16:34 
GeneralRe: Repainting window. Pin
Nick Z.8-Jul-04 15:43
Nick Z.8-Jul-04 15:43 
GeneralRe: Repainting window. Pin
Nick Z.8-Jul-04 16:21
Nick Z.8-Jul-04 16:21 
GeneralRe: Repainting window. Pin
Heath Stewart8-Jul-04 16:38
protectorHeath Stewart8-Jul-04 16:38 
GeneralRe: Repainting window. Pin
Nick Z.8-Jul-04 17:00
Nick Z.8-Jul-04 17:00 
GeneralRe: Repainting window. Pin
Heath Stewart8-Jul-04 17:08
protectorHeath Stewart8-Jul-04 17:08 
GeneralRe: Repainting window. Pin
Nick Z.8-Jul-04 17:51
Nick Z.8-Jul-04 17:51 
GeneralA far more superior solution. Pin
Nick Z.8-Jul-04 18:54
Nick Z.8-Jul-04 18:54 
General"contexMenu" in "TreeView" for specific "Node". Pin
novitzky8-Jul-04 9:50
novitzky8-Jul-04 9:50 
GeneralRe: "contexMenu" in "TreeView" for specific "Node". Pin
Judah Gabriel Himango8-Jul-04 9:59
sponsorJudah Gabriel Himango8-Jul-04 9:59 
GeneralRe: "contexMenu" in "TreeView" for specific "Node". Pin
novitzky8-Jul-04 22:01
novitzky8-Jul-04 22:01 
GeneralRe: "contexMenu" in "TreeView" for specific "Node". Pin
saud_a_k9-Jul-04 1:37
saud_a_k9-Jul-04 1:37 
GeneralReturning a String from Managed to Unmanaged Code Pin
SanShou8-Jul-04 6:25
SanShou8-Jul-04 6:25 
GeneralRe: Returning a String from Managed to Unmanaged Code Pin
Heath Stewart8-Jul-04 8:49
protectorHeath Stewart8-Jul-04 8:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.