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

[Solved] Setting value in unbound checkbox to binding source

2 Answers 227 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ruth Goldberg
Top achievements
Rank 1
Ruth Goldberg asked on 23 Feb 2010, 09:28 PM
I have an editable checkbox on a grid that I want to set to true if another field is "Y" and false otherwise. I can't use an expression because then the checkbox is not editable. I am able to change the data source when the check box is clicked but I'm having a problem initializing the checkbox with the correct value. Which event is the proper place to do so?

2 Answers, 1 is accepted

Sort by
0
Accepted
Jack
Telerik team
answered on 25 Feb 2010, 07:41 AM
Hi Ruth Goldberg,

That's interesting scenario. Currently RadGridView doesn't support it directly. I added this as a feature request and we will consider implementing it in a future version of our grid. I updated also your Telerik points for this feature request. Nevertheless, you can implement the scenario by using a custom column, cell, and editor. Please consider the code snippet below:

this.radGridView1.MasterGridViewTemplate.AutoGenerateColumns = false;
this.radGridView1.Columns.Add(new CustomCheckBoxColumn("Bool"));
this.radGridView1.DataSource = table;
 
public class CustomCheckBoxColumn : GridViewCheckBoxColumn
{
    public CustomCheckBoxColumn(string fieldName)
        : base(fieldName)
    {
    }
 
    public override Type GetCellType(GridViewRowInfo row)
    {
        if (row is GridViewDataRowInfo)
        {
            return typeof(CustomCheckBoxCell);
        }
        return base.GetCellType(row);
    }
}
 
public class CustomCheckBoxCell : GridDataCellElement
{
    private CustomCheckBoxEditor checkBoxEditor;
 
    public CustomCheckBoxCell(GridViewColumn column, GridRowElement row)
        : base(column, row)
    {
    }
 
    protected override Type ThemeEffectiveType
    {
        get { return typeof(GridCheckBoxCellElement); }
    }
 
    public override IInputEditor Editor
    {
        get
        {
            return this.checkBoxEditor;
        }
    }
 
    protected override void OnLoaded()
    {
        base.OnLoaded();
        this.GridControl.EditorManager.RegisterPermanentEditorType(typeof(CustomCheckBoxEditor));
    }
 
    protected override void CreateChildElements()
    {
        base.CreateChildElements();
        this.checkBoxEditor = new CustomCheckBoxEditor();
        this.Children.Add(this.checkBoxEditor.EditorElement);
    }
 
    protected override void SetContentCore(object value)
    {
        if (this.GridControl.IsInEditMode && this.IsCurrent)
        {
            return;
        }
 
        this.checkBoxEditor.BeginInit();
        this.checkBoxEditor.Value = value;
        this.checkBoxEditor.EndInit();
    }
}
 
public class CustomCheckBoxEditor: RadCheckBoxEditor
{
    public override object Value
    {
        get
        {
            if ((bool)base.Value == true)
            {
                return 'Y';
            }
            return 'N';
        }
        set
        {
            if (value is char)
            {
                if ((char)value == 'Y')
                {
                    base.Value = Telerik.WinControls.Enumerations.ToggleState.On;
                }
                else
                {
                    base.Value = Telerik.WinControls.Enumerations.ToggleState.Off;
                }
            }
            else
            {
                base.Value = value;
            }
        }
    }
}

Should you have other questions, don't hesitate to ask.

Kind regards,

Jack
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Ruth Goldberg
Top achievements
Rank 1
answered on 25 Feb 2010, 09:41 PM
Thanks.
Tags
GridView
Asked by
Ruth Goldberg
Top achievements
Rank 1
Answers by
Jack
Telerik team
Ruth Goldberg
Top achievements
Rank 1
Share this question
or