This is a migrated thread and some comments may be shown as answers.

Add checkbox in a specific cell

1 Answer 357 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Simone
Top achievements
Rank 1
Simone asked on 07 Jan 2015, 10:25 AM
Hi,
I have a radGridView control with two columns, the second one should contain string values in every cell except for the last one which should be a checkbox. I'm not able to put the checkbox inside the cell. I've tried using the "CreateCell" event of the gridview in this way:

private void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e)
{
    if (e.Column.Index==1 && e.Row.RowInfo.Index==18)
      {
         e.CellElement = new GridCheckBoxCellElement(e.Column, e.Row);
      }
}

The problem is that e.Row.RowInfo.Index is not the "absolute" row index but it's relative to the displayed cells. So, if I have a resized form the result of e.Row.RowInfo.Index could be any number between 0 and rows.Count.
Can anyone help me? Thanks.

1 Answer, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 09 Jan 2015, 02:04 PM
Hi Simone,

Thank you for writing. 

RadGridView supports the following types of columns by default. However we can create custom cell elements and custom columns which we can fill with data of different type. More information on this approach can be found in the following help article.

To reach the desired behavior I created two classes CustomCellElement and CustomCollumn. My idea was to add in each cell an instance of RadCheckBoxElement which I hidden everywhere except for the second cell in the last row. To make this solution work you would also need to subscribe to CellBeginEdit and CellFormatting events and perform the required checking there. Please find below my code implementation:
public class CustomColumn : GridViewTextBoxColumn
{
    public CustomColumn(string fieldName)
        : base(fieldName)
    {
    }
 
    public override Type GetCellType(GridViewRowInfo row)
    {
        if (row is GridViewDataRowInfo)
        {
            return typeof(CustomCellElement);
        }
        return base.GetCellType(row);
    }
}
public class CustomCellElement : GridDataCellElement
{
    public RadCheckBoxElement RadCheckBoxElement;
 
    public CustomCellElement(GridViewColumn column, GridRowElement row)
        : base(column, row)
    {
    }
 
    protected override void CreateChildElements()
    {
        base.CreateChildElements();
 
        this.RadCheckBoxElement = new RadCheckBoxElement();
        this.Children.Add(this.RadCheckBoxElement);
    }
 
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(GridDataCellElement);
        }
    }
 
    public override bool IsCompatible(GridViewColumn data, object context)
    {
        return data is CustomColumn && context is GridDataRowElement;
    }
}
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        
        this.radGridView1.CellBeginEdit += radGridView1_CellBeginEdit;
        this.radGridView1.CellFormatting += radGridView1_CellFormatting;
        this.SetupGrids();
    }
 
    private void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
    {
        if (e.Row.Index == radGridView1.Rows.Count - 1 && e.Column.Index == 1)
        {
            e.Cancel = true;
        }
    }
 
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        CustomCellElement el = e.CellElement as CustomCellElement;
        if (el != null)
        {
            el.RadCheckBoxElement.Visibility = ElementVisibility.Collapsed;
            if (e.Row.Index == radGridView1.Rows.Count - 1)
            {
                el.RadCheckBoxElement.Visibility = ElementVisibility.Visible;
            }
        }
    }
 
    private void SetupGrids()
    {
        this.radGridView1.Columns.Add(new GridViewDecimalColumn("Id"));
        this.radGridView1.Columns.Add(new CustomColumn("Description"));
 
        for (int i = 0; i < 20; i++)
        {
            GridViewRowInfo rowInfo = this.radGridView1.Rows.AddNew();
            rowInfo.Cells[0].Value = i;
            rowInfo.Cells[1].Value = "Decription " + i;
        }
 
        radGridView1.Rows.Add("20");
        this.radGridView1.BestFitColumns();
    }
}

I am also attaching a .gif file showing how the grid looks on my side.

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Simone
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Share this question
or