public DialogResult setVisibility(DataGridView EditedDataGridView) { try { System.Drawing.Rectangle CellRectangle1 = EditedDataGridView.GetCellDisplayRectangle(2, 2, true); this.Location = EditedDataGridView.PointToClient(new Point(EditedDataGridView.Left + 2 * EditedDataGridView.RowHeadersWidth, EditedDataGridView.Top + 2 * EditedDataGridView.ColumnHeadersHeight)); foreach (DataGridViewColumn CurrentColumn in EditedDataGridView.Columns) { dgvColumns.Rows.Add(CurrentColumn.Name, CurrentColumn.HeaderText, CurrentColumn.DisplayIndex.ToString(), CurrentColumn.Visible, CurrentColumn.AutoSizeMode.ToString(), CurrentColumn.Width.ToString(), CurrentColumn.FillWeight.ToString(), CurrentColumn.MinimumWidth.ToString()); } this.ShowDialog(EditedDataGridView); if(this.DialogResult == System.Windows.Forms.DialogResult.OK) { Int32 ColumnIndex=0; foreach (DataGridViewColumn CurrentColumn in EditedDataGridView.Columns) { CurrentColumn.DisplayIndex = Int32.Parse(dgvColumns.Rows[ColumnIndex].Cells["colDisplayIndex"].Value.ToString()); CurrentColumn.Visible = (Boolean)dgvColumns.Rows[ColumnIndex].Cells["colVisible"].Value; CurrentColumn.AutoSizeMode = (DataGridViewAutoSizeColumnMode)Enum.Parse(typeof(DataGridViewAutoSizeColumnMode), (String)dgvColumns.Rows[ColumnIndex].Cells["colAutoSizeMode"].Value); if(CurrentColumn.AutoSizeMode == DataGridViewAutoSizeColumnMode.Fill) CurrentColumn.FillWeight = Single.Parse(dgvColumns.Rows[ColumnIndex].Cells["colFillWeight"].Value.ToString().Replace(",", "."),System.Globalization.NumberStyles.AllowDecimalPoint,System.Globalization.CultureInfo.InvariantCulture.NumberFormat); else CurrentColumn.Width = Int32.Parse(dgvColumns.Rows[ColumnIndex].Cells["colWidth"].Value.ToString()); CurrentColumn.MinimumWidth = Int32.Parse(dgvColumns.Rows[ColumnIndex].Cells["colMinimumWidth"].Value.ToString()); ColumnIndex++; } } return DialogResult; } catch (Exception ex) { throw new Exception("Error while editing column visibility", ex); } }
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; } } }
/// <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); }