Example E2779
Visible to All Users

WinForms Data Grid - Simultaneous editing of several cell values

This example shows how to edit the values in selected cells at the same time.

Edit Values in Selected Cells - WinForms Data Grid

C#
bool lockEvents; private void OnCellValueChanged(CellValueChangedEventArgs e) { if (lockEvents) return; lockEvents = true; SetSelectedCellsValues(e.Value); lockEvents = false; } private void SetSelectedCellsValues(object value) { try { view.BeginUpdate(); GridCell[] cells = view.GetSelectedCells(); ChangeMode mode = (ChangeMode)radioGroup.EditValue; foreach(GridCell cell in cells) { int rowHandle = cell.RowHandle; GridColumn column = cell.Column; switch(mode) { case ChangeMode.All: break; case ChangeMode.Column: column = view.FocusedColumn; break; case ChangeMode.Row: rowHandle = view.FocusedRowHandle; break; } view.SetRowCellValue(rowHandle, column, value); } } catch(Exception ex) { } finally { view.EndUpdate(); } }

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

MultiSelectionEditingHelper.cs(vb)
C#
using System; using System.Collections.Generic; using System.Windows.Forms; using DevExpress.XtraGrid.Views.Grid; using DevExpress.Utils; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Views.Grid.ViewInfo; using DevExpress.XtraEditors; using DevExpress.XtraGrid.Columns; namespace WindowsApplication1 { public class MultiSelectionEditingHelper { private GridView view; private RadioGroup radioGroup; public MultiSelectionEditingHelper(GridView view, RadioGroup radioGroup) { this.radioGroup = radioGroup; this.view = view; this.view.OptionsBehavior.EditorShowMode = EditorShowMode.MouseDownFocused; this.view.MouseUp += view_MouseUp; this.view.CellValueChanged += view_CellValueChanged; this.view.MouseDown += view_MouseDown; } void view_MouseDown(object sender, MouseEventArgs e) { if (GetInSelectedCell(e)) { GridHitInfo hi = view.CalcHitInfo(e.Location); if (view.FocusedRowHandle == hi.RowHandle) { view.FocusedColumn = hi.Column; DXMouseEventArgs.GetMouseArgs(e).Handled = true; } } } void view_CellValueChanged(object sender, CellValueChangedEventArgs e) { OnCellValueChanged(e); } bool lockEvents; private void OnCellValueChanged(CellValueChangedEventArgs e) { if (lockEvents) return; lockEvents = true; SetSelectedCellsValues(e.Value); lockEvents = false; } private void SetSelectedCellsValues(object value) { try { view.BeginUpdate(); GridCell[] cells = view.GetSelectedCells(); ChangeMode mode = (ChangeMode)radioGroup.EditValue; foreach(GridCell cell in cells) { int rowHandle = cell.RowHandle; GridColumn column = cell.Column; switch(mode) { case ChangeMode.All: break; case ChangeMode.Column: column = view.FocusedColumn; break; case ChangeMode.Row: rowHandle = view.FocusedRowHandle; break; } view.SetRowCellValue(rowHandle, column, value); } } catch(Exception ex) { } finally { view.EndUpdate(); } } private bool GetInSelectedCell(MouseEventArgs e) { GridHitInfo hi = view.CalcHitInfo(e.Location); return hi.InRowCell && view.IsCellSelected(hi.RowHandle, hi.Column); } void view_MouseUp(object sender, MouseEventArgs e) { bool inSelectedCell = GetInSelectedCell(e); if (inSelectedCell) { DXMouseEventArgs.GetMouseArgs(e).Handled = true; view.ShowEditorByMouse(); } } public void Disable() { view.MouseUp -= view_MouseUp; view.CellValueChanged -= view_CellValueChanged; view.MouseDown -= view_MouseDown; view = null; } } public enum ChangeMode { All, Row, Column } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.