private void AddNewKey() { NewRow = true; foreach (DataGridViewRow a in SelectedDataGridView.Rows) { if (AvailableDataGridView.SelectedRows.Count > 0 && a.Cells["id"].Value != null && a.Cells["id"].Value.ToString() == AvailableDataGridView.SelectedRows[0].Cells["id"].Value.ToString()) { NewRow = false; } } if (NewRow) { DataGridViewRow Row = (DataGridViewRow)AvailableDataGridView.SelectedRows[0].Clone(); for (int i = 0; i < AvailableDataGridView.SelectedRows[0].Cells.Count; i++) { Row.Cells[i].Value = AvailableDataGridView.SelectedRows[0].Cells[i].Value; } int idx = SelectedDataGridView.Rows.Add(Row); //SelectedDataGridView.Rows.Add(AvailableDataGridView[0, AvailableDataGridView.CurrentRow.Index].Value.ToString(), AvailableDataGridView[1, AvailableDataGridView.CurrentRow.Index].Value.ToString()); NewRow = true; SelectedDataGridView.ClearSelection(); SelectedDataGridView.Rows[idx].Selected = true; SelectedDataGridView.Rows[idx].Cells[SelectedDataGridView.Columns.Cast <DataGridViewColumn>().First(x => x.Visible).Name].Selected = true; } }
private void DataGridView_MouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Right) { return; } var hti = SelectedDataGridView.HitTest(e.X, e.Y); if (hti.RowIndex < 0 || SelectedDataGridView.Rows[hti.RowIndex].Selected) { return; } SelectedDataGridView.ClearSelection(); SelectedDataGridView.Rows[hti.RowIndex].Selected = true; }
private void AddNewKey() { NewRow = true; foreach (DataGridViewRow a in SelectedDataGridView.Rows) { if (a.Cells["id"].Value.ToString() == AvailabeDataGridView.CurrentRow.Cells["id"].Value.ToString()) { NewRow = false; } } if (NewRow) { int idx = SelectedDataGridView.Rows.Add(AvailabeDataGridView[0, AvailabeDataGridView.CurrentRow.Index].Value.ToString(), AvailabeDataGridView[1, AvailabeDataGridView.CurrentRow.Index].Value.ToString()); NewRow = true; SelectedDataGridView.ClearSelection(); SelectedDataGridView.Rows[idx].Selected = true; SelectedDataGridView.Rows[idx].Cells[SelectedDataGridView.Columns.Cast <DataGridViewColumn>().First(x => x.Visible).Name].Selected = true; } }
private void InstanceContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) { var mousepos = MousePosition; if (!(sender is ContextMenuStrip cms)) { return; } var relMousePos = cms.PointToClient(mousepos); if (cms.ClientRectangle.Contains(relMousePos)) { var dgvRelMousePos = SelectedDataGridView.PointToClient(mousepos); var hti = SelectedDataGridView.HitTest(dgvRelMousePos.X, dgvRelMousePos.Y); if (hti.RowIndex == -1) { e.Cancel = true; } } else { e.Cancel = true; } }