/// <summary> /// Controls the way the datagridview handles row selections /// </summary> /// <param name="sender">Not used</param> /// <param name="e">Mouse event args</param> private void ExceptionRules_MouseDown(object sender, MouseEventArgs e) { DataGridView.HitTestInfo hti; hti = exceptionRulesDGV.HitTest(e.X, e.Y); if (e.Button == MouseButtons.Right) { if (hti.Type == DataGridViewHitTestType.Cell) { if (!((DataGridViewRow)(exceptionRulesDGV.Rows[hti.RowIndex])).Selected) { exceptionRulesDGV.ClearSelection(); ((DataGridViewRow)exceptionRulesDGV.Rows[hti.RowIndex]).Selected = true; } separatorM.Visible = true; removeEntryM.Visible = true; } else { separatorM.Visible = false; removeEntryM.Visible = false; this.exceptionRulesDGV.ClearSelection(); } } if (e.Button == MouseButtons.Left) { if (hti.Type != DataGridViewHitTestType.Cell) { this.exceptionRulesDGV.ClearSelection(); } } }
public override AccessibleObject HitTest(int x, int y) { if (!owner.IsHandleCreated) { return(null); } Point pt = owner.PointToClient(new Point(x, y)); HitTestInfo hti = owner.HitTest(pt.X, pt.Y); switch (hti.Type) { case DataGridViewHitTestType.Cell: return(owner.Rows[hti.RowIndex].Cells[hti.ColumnIndex].AccessibilityObject); case DataGridViewHitTestType.ColumnHeader: // map the column index to the actual display index int actualDisplayIndex = owner.Columns.ColumnIndexToActualDisplayIndex(hti.ColumnIndex, DataGridViewElementStates.Visible); if (owner.RowHeadersVisible) { // increment the childIndex because the first child in the TopRowAccessibleObject is the TopLeftHeaderCellAccObj return(TopRowAccessibilityObject.GetChild(actualDisplayIndex + 1)); } else { return(TopRowAccessibilityObject.GetChild(actualDisplayIndex)); } case DataGridViewHitTestType.RowHeader: return(owner.Rows[hti.RowIndex].AccessibilityObject); case DataGridViewHitTestType.TopLeftHeader: return(owner.TopLeftHeaderCell.AccessibilityObject); case DataGridViewHitTestType.VerticalScrollBar: return(owner.VerticalScrollBar.AccessibilityObject); case DataGridViewHitTestType.HorizontalScrollBar: return(owner.HorizontalScrollBar.AccessibilityObject); default: return(null); } }
public DataGridViewSelection(DataGridView grid) { _rows = grid.SelectedRows.OfType<DataGridViewRow>(); if (!_rows.Any()) { _rows = grid.SelectedCells.OfType<DataGridViewCell>().Select(c => c.OwningRow).Distinct(); var cols = grid.SelectedCells.OfType<DataGridViewCell>().Select(c => c.OwningColumn).Distinct().ToArray(); if (cols.Length == 1) _columnPropertyName = cols[0].DataPropertyName; } if (!_rows.Any() && grid.CurrentCell != null) { _rows = Enumerable.Repeat(grid.CurrentCell.OwningRow, 1); _columnPropertyName = grid.CurrentCell.OwningColumn.DataPropertyName; } else { _rows = Enumerable.Empty<DataGridViewRow>(); _columnPropertyName = null; } var client = grid.PointToClient(Cursor.Position); var hit = grid.HitTest(client.X, client.Y); if (!_rows.Any(r => r.Index == hit.RowIndex)) { if (hit.RowIndex >= 0) { _rows = Enumerable.Repeat(grid.Rows[hit.RowIndex], 1); _columnPropertyName = grid.Columns[hit.ColumnIndex].DataPropertyName; } else { _rows = Enumerable.Empty<DataGridViewRow>(); _columnPropertyName = null; } } }
//show the contextmenu after mouseup to select transform type private void showContextMenus(DataGridView dgv, MouseEventArgs me) { DataGridView.HitTestInfo ht = dgv.HitTest(me.X, me.Y); int intColndx = ht.ColumnIndex; int intRowndx = ht.RowIndex; DataTable _dt = (DataTable)dgvObs.DataSource; if (intRowndx > 0 && intColndx > 0) return; //cell hit, go away //get transform user selected if (intRowndx < 0 && intColndx >= 0) { if (intColndx == 1) { if (!boolObsTransformed) { cmforResponseVar.MenuItems[0].Enabled = true; //we can transform a response variable cmforResponseVar.MenuItems[1].Enabled = false; //but we cannot untransform an untransformed variable } else { cmforResponseVar.MenuItems[0].Enabled = false; //but we cannot transform a transformed response cmforResponseVar.MenuItems[1].Enabled = true; //but we can untransform a transformed response } cmforResponseVar.Show(dgv, new Point(me.X, me.Y)); } } }
private void dgvCommandersLog_Click(object sender, EventArgs e) { DataGridView.HitTestInfo hit; try { m_ClickedDGVArgs = (MouseEventArgs)e; if(m_ClickedDGVArgs.Button == System.Windows.Forms.MouseButtons.Right) { m_ClickedDGV = (DataGridView)sender; hit = m_ClickedDGV.HitTest(m_ClickedDGVArgs.X, m_ClickedDGVArgs.Y); if (hit.Type == DataGridViewHitTestType.TopLeftHeader) { DataGridViewSettings Tool = new DataGridViewSettings(); if(Tool.setVisibility(m_ClickedDGV) == DialogResult.OK) { m_GUIInterface.saveSetting(m_ClickedDGV); } } else if (hit.Type == DataGridViewHitTestType.Cell) { cmsLog.Show(m_ClickedDGV, m_ClickedDGVArgs.Location); } } } catch (Exception ex) { throw new Exception("Error in DataGridView_Click", ex); } }
/// <summary> /// ドロップ先の行の決定 /// </summary> /// <param name="grid"></param> /// <param name="e"></param> /// <param name="from"></param> /// <param name="to"></param> /// <param name="next"></param> /// <returns></returns> private bool DecideDropDestinationRowIndex( DataGridView grid, DragEventArgs e, out int from, out int to, out bool next) { from = (int)e.Data.GetData(typeof(int)); // 元の行が追加用の行であれば、常に false if (grid.NewRowIndex != -1 && grid.NewRowIndex == from) { to = 0; next = false; return false; } Point clientPoint = grid.PointToClient(new Point(e.X, e.Y)); // 上下のみに着目するため、横方向は無視する clientPoint.X = 1; DataGridView.HitTestInfo hit = grid.HitTest(clientPoint.X, clientPoint.Y); to = hit.RowIndex; if (to == -1) { int top = grid.ColumnHeadersVisible ? grid.ColumnHeadersHeight : 0; top += 1; // ... if (top > clientPoint.Y) // ヘッダへのドロップ時は表示中の先頭行とする to = grid.FirstDisplayedCell.RowIndex; else // 最終行へ to = grid.Rows.Count - 1; } // 追加用の行は無視 if (to == grid.NewRowIndex) to--; next = (to > from); return (from != to); }
// user click captured - decide what menu items are appropriate and show them public void showContextMenus(DataGridView dgv, MouseEventArgs me, DataTable dt) { dtColumnInformation dtCI = new dtColumnInformation(dt); Utilities utils = new Utilities(); DataGridView.HitTestInfo ht = dgv.HitTest(me.X, me.Y); int colndx = ht.ColumnIndex; int rowndx = ht.RowIndex; if (rowndx > 0 && colndx > 0) return; //cell hit, go away if (rowndx < 0 && colndx >= 0) { //col header hit, show proper menu intSelectedColIndex = colndx; //do nothing if col 0 selected if (colndx >= 1) { string colname = dt.Columns[colndx].Caption; if (colname == strResponseVarColName) { if (utils.testValueAttribute(dt.Columns[intResponseVarColIndex], VBCommon.Globals.DEPENDENTVAR)) { cmforResponseVar.MenuItems[0].Enabled = true; //we can transform a response variable } else { cmforResponseVar.MenuItems[0].Enabled = false; //but we cannot transform a transformed response } if (utils.testValueAttribute(dt.Columns[intResponseVarColIndex], VBCommon.Globals.DEPENDENTVARIBLETRANSFORM)) { cmforResponseVar.MenuItems[2].Enabled = true; //we can untransform the transformed response variable } else { cmforResponseVar.MenuItems[2].Enabled = false; //but cannot untransform a response variable } cmforResponseVar.Show(dgv, new Point(me.X, me.Y)); } else { //show context menu for ivs if (dtCI.getColStatus(dt.Columns[intSelectedColIndex].ColumnName.ToString())) { //here if col enabled cmforIVs.MenuItems[0].Enabled = true; cmforIVs.MenuItems[1].Enabled = false; //cannot enable enabled col cmforIVs.MenuItems[2].Enabled = true; //response variable must be a ME, T(RV) or I(IV) not created by general transform //if they do this then we're to remove all general operations performed, if (canSetRV(utils)) cmforIVs.MenuItems[2].Enabled = true; else cmforIVs.MenuItems[2].Enabled = false; if (dt.Columns[intSelectedColIndex].ExtendedProperties.ContainsKey(VBCommon.Globals.MAINEFFECT)) cmforIVs.MenuItems[4].Enabled = false; //cannot remove maineffect column else cmforIVs.MenuItems[4].Enabled = true; } else { //here if col disabled cmforIVs.MenuItems[0].Enabled = false; //cannot disable disabled col cmforIVs.MenuItems[1].Enabled = true; cmforIVs.MenuItems[2].Enabled = false; //cannot disabled the response variable } cmforIVs.Show(dgv, new Point(me.X, me.Y)); } } } else if (rowndx >= 0 && colndx < 0) { //row header hit, show menu intSelectedRowIndex = rowndx; if (dtRI.getRowStatus(dt.Rows[intSelectedRowIndex][0].ToString())) { //here if row is enabled cmforRows.MenuItems[0].Enabled = true; cmforRows.MenuItems[1].Enabled = false; //cannot enable enabled row } else { //here if row is disabled cmforRows.MenuItems[0].Enabled = false; //cannot disable disabled row cmforRows.MenuItems[1].Enabled = true; } cmforRows.Show(dgv, new Point(me.X, me.Y)); } }
private void OnDataGridMouseDown(DataGridView sender, MouseEventArgs e) { _row = sender.HitTest(e.X, e.Y).RowIndex; if (_row != -1) { var dragSize = SystemInformation.DragSize; _dragBox = new Rectangle( new Point(e.X - dragSize.Width / 2, e.Y - dragSize.Height / 2), dragSize ); } else { _dragBox = Rectangle.Empty; } }
private void DataGridView_Click(object sender, EventArgs e) { DataGridView dgv2 = null; DataGridView.HitTestInfo hit; try { m_ClickedDGVArgs = (MouseEventArgs)e; if(m_ClickedDGVArgs.Button == System.Windows.Forms.MouseButtons.Right) { m_ClickedDGV = (DataGridView)sender; hit = m_ClickedDGV.HitTest(m_ClickedDGVArgs.X, m_ClickedDGVArgs.Y); if (hit.Type == DataGridViewHitTestType.TopLeftHeader) { DataGridViewSettings Tool = new DataGridViewSettings(); if(m_ClickedDGV.Equals(dgvStation1)) dgv2 = dgvStation2; else if(m_ClickedDGV.Equals(dgvStation2)) dgv2 = dgvStation1; if(Tool.setVisibility(m_ClickedDGV) == DialogResult.OK) { m_GUIInterface.saveSetting(m_ClickedDGV); if(dgv2 != null) { DataGridViewSettings.CloneSettings(ref m_ClickedDGV, ref dgv2); m_GUIInterface.saveSetting(dgv2); } } } else if (hit.Type == DataGridViewHitTestType.Cell) { if(m_ClickedDGV.Equals(dgvStationToStationRoutes)) { contextMenuStrip2.Show(m_ClickedDGV, m_ClickedDGVArgs.Location); } else if(m_ClickedDGV.Equals(dgvAllCommodities)) { contextMenuStrip3.Show(m_ClickedDGV, m_ClickedDGVArgs.Location); } else { contextMenuStrip1.Show(m_ClickedDGV, m_ClickedDGVArgs.Location); } } } } catch (Exception ex) { CErr.processError(ex, "Error while changing DataGridView settings"); } }