Example E2578
Visible to All Users

WinForms Data Grid - Add a column that behaves like a radio group

Each data cell has its own inplace editor. A cell can contain radio group items. You can select different items in different cells. Checking a radio item in one cell does not uncheck items in other cells.

This example shows how to make all cells in a column behave like one radio group.

Files to Review

See Also

Does this example address your development requirements/objectives?

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

Example Code

Form1.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { private DataTable CreateTable(int RowCount) { DataTable tbl = new DataTable(); tbl.Columns.Add("Name", typeof(string)); tbl.Columns.Add("ID", typeof(int)); tbl.Columns.Add("Number", typeof(int)); tbl.Columns.Add("Date", typeof(DateTime)); for(int i = 0; i < RowCount; i++) tbl.Rows.Add(new object[] { String.Format("Name{0}", i), i, 3 - i, DateTime.Now.AddDays(i) }); return tbl; } private GridRadioGroupColumnHelper _Helper; public Form1() { InitializeComponent(); gridControl1.DataSource = CreateTable(20); _Helper = new GridRadioGroupColumnHelper(gridView1); _Helper.SelectedRowChanged += _Helper_SelectedRowChanged; } void _Helper_SelectedRowChanged(object sender, EventArgs e) { Text = _Helper.SelectedDataSourceRowIndex.ToString(); } protected override void OnFormClosing(FormClosingEventArgs e) { base.OnFormClosing(e); _Helper.SelectedRowChanged -= _Helper_SelectedRowChanged; _Helper.Disable(); _Helper = null; } } }
Helper/GridRadioGroupColumnHelper.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraEditors.Repository; using DevExpress.XtraGrid.Columns; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraEditors; namespace WindowsApplication1 { public class GridRadioGroupColumnHelper { public event EventHandler SelectedRowChanged; private GridView _GridView; private RepositoryItemCheckEdit _RepositoryItem = new RepositoryItemCheckEdit(); public RepositoryItemCheckEdit RadioRepositoryItem { get { return _RepositoryItem; } set { _RepositoryItem = value; } } private GridColumn _RadioGroupColumn = new GridColumn(); public GridColumn RadioGroupColumn { get { return _RadioGroupColumn; } set { _RadioGroupColumn = value; } } private int _SelectedDataSourceRowIndex; public int SelectedDataSourceRowIndex { get { return _SelectedDataSourceRowIndex; } set { if (_SelectedDataSourceRowIndex != value) { int oldRowIndex = _SelectedDataSourceRowIndex; _SelectedDataSourceRowIndex = value; SetRowChecked(oldRowIndex, false); OnSelectedRowChanged(oldRowIndex, value); } } } private void SetRowChecked(int dataSourceRowIndex, bool value) { int rowHandle = _GridView.GetRowHandle(dataSourceRowIndex); _GridView.SetRowCellValue(rowHandle, RadioGroupColumn, value); } public GridRadioGroupColumnHelper(GridView gridView) { _GridView = gridView; _GridView.BeginUpdate(); InitGridView(); InitColumn(); InitRepositoryItem(); _GridView.EndUpdate(); } private void InitGridView() { _GridView.CustomUnboundColumnData += _GridView_CustomUnboundColumnData; } void _GridView_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e) { if (e.Column == RadioGroupColumn) { if (e.IsGetData) e.Value = e.ListSourceRowIndex == SelectedDataSourceRowIndex; if (e.IsSetData) if (e.Value.Equals(true)) SelectedDataSourceRowIndex = e.ListSourceRowIndex; } } private void InitColumn() { RadioGroupColumn.FieldName = "RadioGroupColumn"; _GridView.Columns.Add(RadioGroupColumn); RadioGroupColumn.Visible = true; RadioGroupColumn.ColumnEdit = RadioRepositoryItem; RadioGroupColumn.Caption = "Radio"; RadioGroupColumn.UnboundType = DevExpress.Data.UnboundColumnType.Boolean; RadioGroupColumn.MaxWidth = 50; } private void InitRepositoryItem() { RadioRepositoryItem.CheckStyle = DevExpress.XtraEditors.Controls.CheckStyles.Radio; RadioRepositoryItem.EditValueChanged += RadioRepositoryItem_EditValueChanged; _GridView.GridControl.RepositoryItems.Add(RadioRepositoryItem); } void RadioRepositoryItem_EditValueChanged(object sender, EventArgs e) { _GridView.PostEditor(); } private void OnSelectedRowChanged(int oldValue, int newValue) { RaiseSelectedRowChanged(); } protected virtual void RaiseSelectedRowChanged() { if (SelectedRowChanged != null) SelectedRowChanged(_GridView, EventArgs.Empty); } public void Disable() { RadioRepositoryItem.EditValueChanged -= RadioRepositoryItem_EditValueChanged; _GridView.CustomUnboundColumnData -= _GridView_CustomUnboundColumnData; _GridView = null; RadioRepositoryItem = null; } } }

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.