CheckBoxColumns in RadGridView, Multiple CheckBoxColumns

3 posts, 1 answers
  1. KawaUser
    KawaUser avatar
    57 posts
    Member since:
    Oct 2010

    Posted 03 Nov 2010 Link to this post

    I need the checkboxes that have been checked and the data in the database has been changed to stay checked or unchecked. So, if the database says 1 that equals true and the checkbox needs to be checked on grid load and vice versa.  I have the field name set to the column name from the sql table. 

    Basically, the problem is when I load the grid all the checkboxes are unchecked, when some of them should or should not be checked.

    When the user clicks the checkbox it sets the data in the sql table to 1 or 0. 1 if checked. 0 if unchecked.

    Thanks,
    Charles Hawk
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 07 Nov 2010 Link to this post

    Hello,

    If i understood correctly, and you have an int field in the db that holds just values 0 or 1.
    If this is true then i would suggest changing that field to Boolean, because like that it will bind directly as a check box column on the grid, but if you cannot do this, then you can try the following:
    The first one and the easiest, if you are creating the data set / data table by hand you can just specify that the type of that column is bool, like so:
    var dataTable = new DataTable();
    dataTable.Columns.Add(new DataColumn("Changed", typeof(bool)));
    dataTable.Columns.Add(new DataColumn("Name"));
    dataTable.Rows.Add(0, "asda");
    dataTable.Rows.Add(1, "asda2");
    dataTable.Rows.Add(1, "asda3");
    this.radGridView1.DataSource = dataTable;

    The other one would be on DataBindingComplete event to create a GridViewCheckBoxColumn and bind that column to the FieldName, something like this:
    using System;
    using System.Data;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    public partial class Form1 : Form
    {
        private RadGridView radGridView1;
     
        public Form1()
        {
            InitializeComponent();
            this.Controls.Add(radGridView1 = new RadGridView());
            this.radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
            radGridView1.Dock = DockStyle.Fill;
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
     
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            var dataTable = new DataTable();
            dataTable.Columns.Add(new DataColumn("Changed", typeof(int)));
            dataTable.Columns.Add(new DataColumn("Name", typeof(string)));
            dataTable.Rows.Add(0, "asda");
            dataTable.Rows.Add(1, "asda2");
            dataTable.Rows.Add(1, "asda3");
            this.radGridView1.DataSource = dataTable;
        }
     
        void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
        {
            radGridView1.Columns["Changed"].IsVisible = false;
     
            var checkBoxColumn = new GridViewCheckBoxColumn("Checked");
            checkBoxColumn.FieldName = "Changed";
     
            radGridView1.Columns.Add(checkBoxColumn);
            this.radGridView1.BestFitColumns();
        }
    }

    If you need any more help please let me know, and if this didn't answer your question please post here a small example so that we'll be able to get to the answer faster.

    Best Regards,
    Emanuel Varga
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Alexander
    Admin
    Alexander avatar
    306 posts

    Posted 09 Nov 2010 Link to this post

    Hello Charles,

    RadGridView creates GridViewDecimalColumn for integer data and GridViewCheckBoxColumn for boolean data. Emanuel explains in his answer how to hide the auto-generated decimal column and use a checkBox column instead of it. The the other option is to use columns created in code instead of the those created automatically by RadGridView.

    Please give us more details about your scenario if you meet additional difficulties to achieve it.

    Best regards,
    Alexander
    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
Back to Top