Home Forums WinForms controls Xceed Grid for WinForms Add Radio Button to Xceed Grid. Reply To: Add Radio Button to Xceed Grid.

Xceed Support
Member
Post count: 5658

In this case, you will need to handle your own selection. Because, when you will click on an other row’s RadioButton, the GridControl’s selection will be lost. Here, I change the BackColor of the selected row to Red.

//Sample code:

public Form1()
{
  InitializeComponent();
}

private void Form1_Load( object sender, EventArgs e )
{
  Column col1 = new Column( “Column1” );
  Column col2 = new Column( “Column2” );
  Column col3 = new Column( “Column3” );
  Column col4 = new Column( “Column4”, typeof( bool ) );
  gridControl1.Columns.Add( col1 );
  gridControl1.Columns.Add( col2 );
  gridControl1.Columns.Add( col3 );
  gridControl1.Columns.Add( col4 );

  for( int i = 0; i < 20; i++ )
  {
    Xceed.Grid.DataRow row = gridControl1.DataRows.AddNew();
    row.Cells[ 0 ].Value = “Item ” + i.ToString();
    row.Cells[ 1 ].Value = “Item ” + i.ToString();
    row.Cells[ 2 ].Value = “Item ” + i.ToString();
    row.Cells[ 3 ].Value = false;

    row.EndEdit();
  }
 
  RadioButton myRad = new RadioButton();
  RadioButton myRadEd = new RadioButton();
  gridControl1.Columns[ 3 ].CellViewerManager = new CellViewerManager( myRad, “Checked” );
  gridControl1.Columns[ 3 ].CellEditorManager = new CellEditorManager( myRadEd, “Checked”, true, true );
  gridControl1.Columns[ 3 ].CellEditorManager.ActivatingControl += new CellEditorEventHandler( CellEditorManager_ActivatingControl );
  gridControl1.Columns[ 3 ].CellEditorManager.DeactivatingControl += new CellEditorEventHandler( CellEditorManager_DeactivatingControl );
}

void CellEditorManager_ActivatingControl( object sender, CellEditorEventArgs e )
{
  ( ( RadioButton )e.Control ).CheckedChanged += new EventHandler( Form1_CheckedChanged );
}

void Form1_CheckedChanged( object sender, EventArgs e )
{
  ( ( RadioButton )sender ).CheckedChanged -= new EventHandler( Form1_CheckedChanged );
  ( ( Xceed.Grid.DataRow )gridControl1.CurrentRow ).EndEdit();
}

private int index = -1;

void CellEditorManager_DeactivatingControl( object sender, CellEditorEventArgs e )
{
  if( ( ( RadioButton )e.Control ).Checked )
  {
    if( index == -1 )
    {
      index = ( ( Xceed.Grid.DataRow )gridControl1.CurrentRow ).Index;
      gridControl1.CurrentRow.BackColor = Color.Red;
    }
    else
    {
      gridControl1.DataRows[ index ].Cells[ 3 ].Value = false;
      gridControl1.DataRows[ index ].ResetBackColor();
      index = ( ( Xceed.Grid.DataRow )gridControl1.CurrentRow ).Index;
      gridControl1.CurrentRow.BackColor = Color.Red;
    }
  }
}

Imported from legacy forums. Posted by CharlesB (had 612 views)