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

Tristate CheckBox column

1 Answer 308 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 04 Oct 2010, 08:35 PM
Hi,

I want to create a checkbox column in my grid which will be databound to a nullable boolean property in the data source.  How do I go about doing this?  I don't see an IsThreeState property on GridViewCheckBoxColumn or RadCheckBoxEditor.


Thanks!

1 Answer, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 04 Oct 2010, 09:22 PM
Hello Matt,

Please check out the following example, it will do exactly what you want, in short in order to do this, you should handle the DataBindingComplete event, and there, get the GridViewCheckBoxColumn you want to make three state and just set:
void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
{
    var checkBoxColumn = (GridViewCheckBoxColumn)((RadGridView)sender).Columns["IsChecked"];
    checkBoxColumn.ThreeState = true;
}


The full example as follows:
using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
 
    using Telerik.WinControls.UI;
 
    public partial class Form1 : Form
    {
        private RadGridView radGridView1;
 
        public Form1()
        {
            InitializeComponent();
            this.Size = new Size(500, 400);
            this.Load += new EventHandler(Form1_Load);
        }
 
        void Form1_Load(object sender, EventArgs e)
        {
            radGridView1 = new RadGridView();
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.Dock = DockStyle.Fill;
            radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
 
            this.Controls.Add(radGridView1);
 
            radGridView1.DataSource = new TestsCollection(1000);
        }
 
        void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
        {
            var checkBoxColumn = (GridViewCheckBoxColumn)((RadGridView)sender).Columns["IsChecked"];
            checkBoxColumn.ThreeState = true;
        }
    }
 
    #region Helpers
 
    internal class Test
    {
        public int Id { get; set; }
 
        public string OtherColumn { get; set; }
 
        public bool? IsChecked { get; set; }
 
        public Test(int id, string otherColumn, bool? isChecked)
        {
            this.Id = id;
            this.OtherColumn = otherColumn;
            this.IsChecked = isChecked;
        }
    }
 
    internal class TestsCollection : BindingList<Test>
    {
        public TestsCollection(int noItems)
        {
            for (int i = 0; i < noItems; i++)
            {
                this.Add(new Test(i, "item" + i, i % 2 == 0 ? true : (bool?)null));
            }
        }
    }
 
    #endregion Helpers

Hope this helps, if you have any more questions please do not hesitate to say so,

Best Regards,
Emanuel Varga
Tags
GridView
Asked by
Matt
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Share this question
or