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

bound checkbox column

7 Answers 136 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Carisch
Top achievements
Rank 1
Carisch asked on 30 Mar 2011, 11:13 PM
This seems like such a simple task, I am really stumped.  Oh well, I want a column to be check boxes in my grid view.  I'm playing around with the radGridView1_CreateCell event with this code.

if (e.Row is GridDataRowElement && e.Column is GridViewDataColumn && ((GridViewDataColumn)e.Column).Name == "MyField")
{
    e.CellType = typeof(RadTextBoxEditor);
}

This gives me an error of:

{"Constructor on type 'Telerik.WinControls.UI.RadTextBoxEditor' not found."}


Any help would be appreciated.  My end goal would be to have a column full of checked and unchecked boxes.
When a value changes(the user clicks), I'm going to update my database.

7 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 31 Mar 2011, 08:33 AM
Hello Carish,

I don't really understand why if you want checkboxes are you giving in the CellType property the type of an editor and more than this a type of a TextBox Editor.

I would suggest a different approach, either creating adding a CheckBoxColumn to the Columns collection, or just add a bool property to the databound object.

If you are looking for something else, could you please provide a small sample (you cannot attach projects here but you can just copy paste the code in a code block) from which i would be able to understand what you are trying to achieve.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 31 Mar 2011, 09:11 AM
Hello,

In addition to Emanuel's comments, you can also find out more about the GridViewCheckBoxColumn at this help topic.

Hope you find this of help
Richard
0
Carisch
Top achievements
Rank 1
answered on 31 Mar 2011, 01:33 PM
Richard, thanks for your comment.  That article would help if I wanted to create a new column, and I'm doing that in other areas, but I want a column which is bound to a datasource, and this datasource has a bool value.  Standard binding just shows "True" or "False".  I want a checkbox.

Ideas?
0
Carisch
Top achievements
Rank 1
answered on 31 Mar 2011, 01:35 PM
Sorry, terrible job getting my code out of my project and onto this page.  It was supposed to be:
private void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e)
{
    if (e.Row is GridDataRowElement && e.Column is GridViewDataColumn && ((GridViewDataColumn)e.Column).Name == "MyField")
    {
        e.CellType = typeof(CustomGridCheckBoxCellElement);
    }
}

This field "MyField" is already of type bool.   I would have thought that by default the gridview would see a bool, and convert it into a checkbox column.

Ideas?
0
Carisch
Top achievements
Rank 1
answered on 31 Mar 2011, 01:39 PM
I should add that I tried:

e.CellType = typeof(RadCheckBoxEditorElement);

and 

e.CellType = typeof(RadCheckBoxEditor);

and 

e.CellType = typeof(GridCheckBoxCellElement);

So I tried creating my own custom cell element object here

public class CustomGridCheckBoxCellElement : GridDataCellElement
{
    RadCheckBoxElement check;
    public CustomGridCheckBoxCellElement(GridViewDataColumn column, GridRowElement row)
        : base(column, row)
    {
    }
    protected override void InitializeFields()
    {
        base.InitializeFields();
        check = new RadCheckBoxElement();
        this.Children.Add(check);
    }
    protected override void SetContentCore(object value)
    {
        if (value != null && value != DBNull.Value && value.ToString().StartsWith("http"))
        {
            check.Checked = Convert.ToBoolean(value);
        }
        else
        {
            check.Checked = false;
        }
    }
}


This worked to display the value as a checkbox, but as soon as you try to edit it, it changes back to text.  Very odd behavior.

0
Carisch
Top achievements
Rank 1
answered on 31 Mar 2011, 01:52 PM
Richard, actually I overlooked a very basic step in creating columns.  I always create my list of columns like this:
radGridView1.Columns.Add("MyField", "Field Header Text", "MyField");

But when I want a column other than the default text box cell, you can use the article you linked and have this

GridViewCheckBoxColumn check = new GridViewCheckBoxColumn();
check.FieldName = "MyField";
check.Name = "MyField";
check.HeaderText = "My Field ?";
radGridView1.Columns.Add(myField);

This is exactly what I wanted, and I knew it was simple.   Thank you very much.
0
Richard Slade
Top achievements
Rank 2
answered on 31 Mar 2011, 02:04 PM
Hello,

Glad that helped. If you have AutoGenerateColumns = true, then your bool column would just come out as a checkbox. As you are defining the columns, as you have mentioned, you specifically need to define the column type.

If you have any other questions, just let me know
Richard
Tags
GridView
Asked by
Carisch
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Richard Slade
Top achievements
Rank 2
Carisch
Top achievements
Rank 1
Share this question
or