New to Telerik UI for WinForms? Download free 30-day trial

RadButtonTextBoxColumn in RadGridView

Environment

Product Version Product Author
2020.2.512 RadGridView for WinForms Nadya Karaivanova

Description

A common requirement is to display a button and text box in GridViewDataColumn. This article demonstrated how you can create RadButtonTextBoxColumn based on the functionality that RadGridView offers.

change-font-radsavefiledialog.png

Solution

RadGridView provides a flexible mechanism for creating custom cell types with custom content elements. Thus, you can create a custom cell with RadButtonTextBoxElement in it and use this cell in a custom column in the grid. Following the steps in the provided in Creating custom cells article, it is possible to create RadButtonTextBoxColumn. Below is a sample demonstration how you can achieve this. Feel free to extend it so that suits your requirements the best.

 public partial class RadForm1 : Telerik.WinControls.UI.RadForm
 {
     public RadForm1()
     {
         InitializeComponent();
         this.radGridView1.AllowAddNewRow = false;
         RadButtonTextBoxColumn customColumn = new RadButtonTextBoxColumn("ButtonTextBoxColumn");
         this.radGridView1.Columns.Add(customColumn);
         this.radGridView1.Columns[0].Width = 250;
         for (int i = 1; i <= 10; i++)
         {
             this.radGridView1.Rows.Add("Item" + i);
         }
     }
 }
 public class ButtonTextBoxCell : GridDataCellElement
 {
     public ButtonTextBoxCell(GridViewColumn column, GridRowElement row) : base(column, row)
     {
     }

     private RadButtonTextBoxElement buttonTextBoxElement;
     protected override void CreateChildElements()
     {
         base.CreateChildElements();
         buttonTextBoxElement = new RadButtonTextBoxElement();
         RadButtonElement buttonElement1 = new RadButtonElement();
         RadButtonElement buttonElement2 = new RadButtonElement();
         buttonElement1.Text = "Edit";
         buttonElement2.Text = "Delete";
         buttonTextBoxElement.RightButtonItems.Add(buttonElement1);
         buttonTextBoxElement.RightButtonItems.Add(buttonElement2);
         this.Children.Add(buttonTextBoxElement);

     }

     protected override void SetContentCore(object value)
     {
         this.buttonTextBoxElement.Text = this.Value.ToString();
     }

     public override bool IsCompatible(GridViewColumn data, object context)
     {
         return data is RadButtonTextBoxColumn && context is GridDataRowElement;
     }
     protected override Type ThemeEffectiveType
     {
         get
         {
             return typeof(GridDataCellElement);
         }
     }
 }
 public class RadButtonTextBoxColumn : GridViewDataColumn
 {
     public RadButtonTextBoxColumn(string fieldName) : base(fieldName)
     {
     }

     public override Type GetCellType(GridViewRowInfo row)
     {
         if (row is GridViewDataRowInfo)
         {
             return typeof(ButtonTextBoxCell);
         }
         return base.GetCellType(row);
     }
 }
In this article