Note that there are some explanatory texts on larger screens.

plurals
  1. POSingle column DGV to accept clipboard data
    primarykey
    data
    text
    <p>I've set a windowsform up. Currently it it has a single DataGridView that is linked to a single table in a SQL Server db. I can browse the current data in the table.</p> <p>How do I set things up so that a user can copy and paste a single column of data from an Excel sheet into the DGV ?</p> <p>If, in Excel I have 'x' in A1 and 'y' in A2 then this must preserve the number of rows when pasted into the DGV i.e for this example it would still be over 2 rows</p> <p>I've tried to adapt the following from <a href="http://www.codeproject.com/Articles/36850/DataGridView-Copy-and-Paste" rel="nofollow">Code Project</a>. If fails on the line if <code>(oCell.Value.ToString() != sCells[i])</code> with a <code>NullReferenceException was unhandled</code> What am I doing wrong?</p> <pre><code> private void uxChargeBackDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { PasteClipboard(); //uxChargeBackDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Clipboard.GetText(); } private void PasteClipboard() { try { string s = Clipboard.GetText(); string[] lines = s.Split('\n'); int iFail = 0, iRow = uxChargeBackDataGridView.CurrentCell.RowIndex; int iCol = uxChargeBackDataGridView.CurrentCell.ColumnIndex; DataGridViewCell oCell; foreach (string line in lines) { if (iRow &lt; uxChargeBackDataGridView.RowCount &amp;&amp; line.Length &gt; 0) { string[] sCells = line.Split('\t'); for (int i = 0; i &lt; sCells.GetLength(0); ++i) { if (iCol + i &lt; this.uxChargeBackDataGridView.ColumnCount) { oCell = uxChargeBackDataGridView[iCol + i, iRow]; if (!oCell.ReadOnly) { if (oCell.Value.ToString() != sCells[i]) { oCell.Value = Convert.ChangeType(sCells[i], oCell.ValueType); oCell.Style.BackColor = Color.Tomato; } else iFail++; //only traps a fail if the data has changed //and you are pasting into a read only cell } } else { break; } } iRow++; } else { break; } if (iFail &gt; 0) MessageBox.Show(string.Format("{0} updates failed due" + " to read only column setting", iFail)); } } catch (FormatException) { MessageBox.Show("The data you pasted is in the wrong format for the cell"); return; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload