private void okButton_Click(object sender, System.EventArgs e) { //if (DT.Select("LNG_Key = '" + this.Key + "'").Length > 0) { //MessageBox.Show(file[this.Key]); if (file.ContainsKey(this.Key)) { MessageBox.Show("Please use a unique key, the one you have provided is already taken", "Unique ID", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); this.DialogResult = DialogResult.None; return; } }
private void lngDataGridView_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.C) { DataObject d = ((DataGridView)sender).GetClipboardContent(); Clipboard.SetDataObject(d); e.Handled = true; } else if (e.Control && e.KeyCode == Keys.V) { DataGridView grid = ((DataGridView)sender); char[] rowSplitter = { '\r', '\n' }; char[] columnSplitter = { '\t' }; //get the text from clipboard IDataObject dataInClipboard = Clipboard.GetDataObject(); string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.Text); if (stringInClipboard == null) { e.Handled = true; return; } //split it into lines string[] rowsInClipboard = stringInClipboard.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries); //get the row and column of selected cell in grid int r = grid.SelectedCells[0].RowIndex; int c = grid.SelectedCells[0].ColumnIndex; if (((DataGridView)sender).SelectedCells.Count > 1) { foreach (DataGridViewCell cell in ((DataGridView)sender).SelectedCells) { if (cell.RowIndex <= r && cell.RowIndex >= 0) { r = cell.RowIndex; if (cell.ColumnIndex < c && cell.ColumnIndex >= 0) { c = cell.ColumnIndex; } } } } DataTable table = (DataTable)grid.DataSource; DataTable tableO = table.Copy(); System.Collections.Generic.Dictionary <int, string[]> cellIndexes = new System.Collections.Generic.Dictionary <int, string[]>(); // loop through the lines, split them into cells and place the values in the corresponding cell. for (int iRow = 0; iRow < rowsInClipboard.Length; iRow++) { //split row into cell values string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter); //cycle through cell values if (grid.RowCount - 1 >= r + iRow) { for (int iCol = 0; iCol < valuesInRow.Length; iCol++) { //assign cell value, only if it within columns of the grid if (grid.ColumnCount - 1 >= c + iCol) { try { //table.Rows.IndexOf(table.DefaultView[r + iRow].Row); //MessageBox.Show(r + iRow + " " + table.DefaultView[r + iRow]); cellIndexes.Add(table.Rows.IndexOf(table.DefaultView[r + iRow].Row), new string[2] { grid.Columns[c + iCol].Name, valuesInRow[iCol] }); } catch {} } else { break; } } } else { break; } } string sort = table.DefaultView.Sort; table.DefaultView.Sort = ""; foreach (System.Collections.Generic.KeyValuePair <int, string[]> index in cellIndexes) { if (index.Value[0] == "LNG_Key") { if (lngFile.ContainsKey(index.Value[1])) { continue; } lngFile.Remove((string)table.Rows[index.Key][0], false); lngFile.Add(index.Value[1], (string)table.Rows[index.Key][1]); } else { lngFile.Add((string)table.Rows[index.Key][0], index.Value[1]); } table.Rows[index.Key].BeginEdit(); table.Rows[index.Key][index.Value[0]] = index.Value[1]; table.Rows[index.Key].EndEdit(); } grid.CurrentCell.Selected = false; grid.CurrentCell = grid.Rows[r].Cells[c]; table.DefaultView.Sort = sort; e.Handled = true; } }