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

Disabling a command button in a gridview

1 Answer 638 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 26 Apr 2012, 01:02 PM
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?

1 Answer, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 27 Apr 2012, 11:41 AM
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
Tags
GridView
Asked by
Matt
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Share this question
or