Allow editing of one (or more) columns

5 posts, 1 answers
  1. Bob
    Bob avatar
    41 posts
    Member since:
    Mar 2012

    Posted 02 May 2012 Link to this post

    FYI - I am new to Telerik controls and new to entity framework 4.0.
    Although I think I have my entities set up ok for now, I am trying to now use the Telerik dataGrid.

    The scenario:
    I set the datasource of the datagrid to that of my entity.
    I do not want the users to be able to edit any of the data.

    I added a checkbox in the first column at design time allowing users to select a particular row for later processing.

    The questions:
    Since the grid is 'not editable' how do I allow the user to select the checkbox to select the row?
    If the grid allow column swapping, is it possible to keep the checkbox I added in the first position?

    Bonus question:
    Once I select the rows I want, what is the preferred method in looping through the rows to gather the rows selected?

    Help is greatly appreciated!
    Bob
  2. Bob
    Bob avatar
    41 posts
    Member since:
    Mar 2012

    Posted 02 May 2012 Link to this post

    OK -

    So I've been playing with the grid and was getting frustrated on the 'Enable Editing' property on the grid versus the column.ReadOnly property.
    Apparently, to make specific fields editable, the 'enable editing' property on the grid must be checked and then you have to spin through columns to enable or disable the read only property.

    Very confusing if you ask me.

    Is there a more efficient way of doing this?
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 03 May 2012 Link to this post

    Hi Bob, 

    Here is a brief sample for you. It should also answer your question regarding looping through the grid too. 
    A note about this.. looping through the ChildRows collection will ensure that you get exactly what the grid is displaying. So if you have filtered the grid, looping over the child rows will give you the filtered (visible) rows. 

    Designer File
    namespace RadControlsWinFormsApp1
    {
        partial class RadForm1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
     
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
     
            #region Windows Form Designer generated code
     
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.radButton1 = new Telerik.WinControls.UI.RadButton();
                this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
                ((System.ComponentModel.ISupportInitialize)(this.radButton1)).BeginInit();
                ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
                ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
                this.SuspendLayout();
                //
                // radButton1
                //
                this.radButton1.Dock = System.Windows.Forms.DockStyle.Bottom;
                this.radButton1.Location = new System.Drawing.Point(0, 359);
                this.radButton1.Name = "radButton1";
                this.radButton1.Size = new System.Drawing.Size(343, 24);
                this.radButton1.TabIndex = 5;
                this.radButton1.Text = "Show Selected";
                //
                // radGridView1
                //
                this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.radGridView1.Location = new System.Drawing.Point(0, 0);
                this.radGridView1.Name = "radGridView1";
                this.radGridView1.Size = new System.Drawing.Size(343, 359);
                this.radGridView1.TabIndex = 6;
                this.radGridView1.Text = "radGridView1";
                //
                // RadForm1
                //
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(343, 383);
                this.Controls.Add(this.radGridView1);
                this.Controls.Add(this.radButton1);
                this.Name = "RadForm1";
                //
                //
                //
                this.RootElement.ApplyShapeToControl = true;
                this.Text = "RadForm1";
                this.ThemeName = "ControlDefault";
                ((System.ComponentModel.ISupportInitialize)(this.radButton1)).EndInit();
                ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
                ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
                this.ResumeLayout(false);
     
            }
     
            #endregion
     
            private Telerik.WinControls.UI.RadButton radButton1;
            private Telerik.WinControls.UI.RadGridView radGridView1;
     
        }
    }

    Form
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Telerik.WinControls;
    using Telerik.WinControls.UI;
    using System.Linq;
     
    namespace RadControlsWinFormsApp1
    {
        public partial class RadForm1 : Telerik.WinControls.UI.RadForm
        {
     
     
            public RadForm1()
            {
                InitializeComponent();
                    this.radButton1.Click +=new EventHandler(radButton1_Click);
     
                List<User> users = new List<User>();
                int k = 0;
                while (k <= 20)
                {
                    bool hasBeard = (k % 2 == 0);
                    users.Add(new User(k, "User " + k.ToString(), hasBeard));
                    k++;
                }
     
                this.radGridView1.DataSource = users;
     
                this.radGridView1.ReadOnly = false;
                this.radGridView1.AllowAddNewRow = false;
                foreach (GridViewDataColumn column in this.radGridView1.Columns)
                {
                    column.ReadOnly = (column.Name!="HasBeard");
                }
            }
     
            void radButton1_Click(object sender, EventArgs e)
            {
                System.Text.StringBuilder s = new StringBuilder();
                foreach (GridViewRowInfo row in this.radGridView1.ChildRows)
                {
                    if ((bool)row.Cells["HasBeard"].Value == true)
                    {
                        s.Append(row.Cells["Name"].Value.ToString() + " has a beard" + Environment.NewLine);
                    }
                }
                RadMessageBox.Show(s.ToString());
            }
     
        }
     
        public class User
        {
            public User(int id, string name, bool hasBeard)
            {
                Id = id;
                Name = name;
                HasBeard = hasBeard;
            }
     
            public User()
            { }
     
            public int Id
            {
                get;
                set;
            }
     
            public string Name
            {
                get;
                set;
            }
     
            public bool HasBeard
            {
                get;
                set;
            }
     
        }
     
    }

    Hope that helps
    Richard
  5. Bob
    Bob avatar
    41 posts
    Member since:
    Mar 2012

    Posted 03 May 2012 Link to this post

    Thanks for the example.  It shows me exactly what I need it to do.
    Bob
  6. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 07 May 2012 Link to this post

    Hi Bob and Richard,

    Thanks to both of you for writing.

    1. I would try to explain the read only case. There are three levels where you can set the ReadOnly property in a RadGridView - on a RadGridView level, on a column level and on a cell level. Setting the ReadOnly property on a higher level means that you cannot override it on a lower level. That said, to achieve your scenario you have to keep the grid editable and only make the columns read only. The best place to do this is right after you set the grid's data source, where you can simply make all columns read only in a foreach loop. Then you just add the check box column and you are done.
    2. RadGridView allows columns to be pinned to the left or right side. You can read more about this  in our product documentation.
    3. In regards to the bonus question, you can do several things:
    3.1 If you will not be working with large number of rows in the grid (e.g. less than 10 000) you can simply iterate the grid Rows collection and pick out the rows that are checked - this is what Richard demonstrated in his post.
    3.2 A more generic approach is to keep a collection of the selected rows. You can subscribe to the CellValueChanged event and each time a row is checked/unchecked you can add/remove it from a collection.

    I hope this will be useful for you. Should you have further questions, I would be glad to help.
     
    All the best,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready