Disabling a command button in a gridview

2 posts, 1 answers
  1. Matt
    Matt avatar
    15 posts
    Member since:
    Jan 2009

    Posted 26 Apr 2012 Link to this post

    I have a grid with a column of GridViewCommandColumn and I wish to programmatically disable certain buttons.

    I'm after something akin to;

    foreach (Telerik.WinControls.UI.GridViewRowInfo gridItem in HeaderGridview.Rows)
    {
        if (gridItem.Cells["Send"].Value.ToString() == "TO BE SENT")
            gridItem.Cells["Resend"].Enabled = false;
     }

    I've googled it to death and can't find any samples anywhere.

    Anyone got an ideas?

  2. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 27 Apr 2012 Link to this post

    Hello Matt, 

    You can disable a button (or indeed the cell) using the CellFormatting event. Please consider the following sample that disables the cell if the Id of the record is 1. 


        public partial class Form1 : RadForm
        {
            List<Item> _items = new List<Item>();
     
            public Form1()
            {
                InitializeComponent();
     
                _items.Add(new Item(1, "Item1"));
                _items.Add(new Item(2, "Item2"));
                _items.Add(new Item(3, "Item3"));
                _items.Add(new Item(4, "Item4"));
                _items.Add(new Item(5, "Item5"));
                _items.Add(new Item(6, "Item6"));
                _items.Add(new Item(7, "Item7"));
     
                this.radGridView1.DataSource = _items;
                this.radGridView1.Columns.Add(new GridViewCommandColumn("My Command"));
                this.radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);
            }
     
            void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
            {
                e.CellElement.Enabled = true;
                if (e.RowIndex > -1 && e.Column.Name == "My Command")
                {
                    if (Convert.ToInt32(e.Row.Cells["Id"].Value) == 1)
                    {
                        e.CellElement.Enabled = false;
                    }
                    else
                    {
                        e.CellElement.Enabled = true;
                    }
                }
            }
     
        }
     
     
        public class Item
        {
            public Item()
            { }
     
            public Item(int id, string name)
            {
                Id = id;
                Name = name;
            }
     
            public int Id
            { get; set; }
     
            public string Name
            { get; set; }
        }

    Please remember to mark as answer if this helps, or just let me  know if you need further help
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top