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

dynamically disabling buttons in gridview

7 Answers 174 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Paul R.
Top achievements
Rank 1
Paul R. asked on 04 Mar 2011, 04:44 PM
Hi There.  I'm trying to dynamically disable/enable buttons in the grid but every command is becoming disabled...  
any ideas?

Thanks.
 


private
void gvMainInfo_CellFormatting(object sender, CellFormattingEventArgs e)
 {
            if (e.CellElement.Value != null)
            {
                if ((e.CellElement.ColumnInfo).HeaderText == "Status")
                {
                    GridCommandCellElement commandCell = (GridCommandCellElement)e.CellElement;
                    RadButtonElement buttonElement = (RadButtonElement)commandCell.Children[0];
 
 
                    if (e.CellElement.Value.ToString() == "Acitvate")
                    {
                            buttonElement.Enabled = true;
                     }
                    else
                    {
                        buttonElement.Enabled = false;
                    }
    }
}

7 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 04 Mar 2011, 04:50 PM
Hello Paul,

Because the grid uses a virtualization system where cells are re-used as you scroll up and down the grid, you will need to reset (or re-enable) your buttons wherever it doesn't meet the condition. The prinicipal is the same as setting styles such as back color when using CellFormatting as per this help topic

Hope that helps
Richard
0
Paul R.
Top achievements
Rank 1
answered on 04 Mar 2011, 05:43 PM
-- duplicate post removed...
0
Paul R.
Top achievements
Rank 1
answered on 04 Mar 2011, 05:47 PM
Hi Richard, thanks for the prompt reply.

I tried applying the information provided in the link but it was to no avail.


The only time I can re-enable the submit button obviously is when the e.CellElement is a GridCommandCellElement.  I think I'm missing something here.


Thanks again.





0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 04 Mar 2011, 06:10 PM
Hi Paul,

If my understanding is correct, then your first code should be fine. Please have a look at this small sample that you can paste into a new project.

Form1 designer
namespace RadControlsWinFormsApp2
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components;
  
        /// <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.radGridView1 = new Telerik.WinControls.UI.RadGridView();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // 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(284, 262);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.Text = "radGridView1";
            this.radGridView1.CellFormatting += new Telerik.WinControls.UI.CellFormattingEventHandler(this.radGridView1_CellFormatting);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.radGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            this.ResumeLayout(false);
  
        }
  
        #endregion
  
        private Telerik.WinControls.UI.RadGridView radGridView1;
    }
}

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
  
namespace RadControlsWinFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
            this.radGridView1.Columns.Add(new GridViewTextBoxColumn("A"));
            this.radGridView1.Columns.Add(new GridViewTextBoxColumn("B"));
  
            GridViewCommandColumn commandColumn = new GridViewCommandColumn();
            commandColumn.Name = "Button";
            commandColumn.UseDefaultText = false;
            commandColumn.HeaderText = "Button";
            this.radGridView1.Columns.Add(commandColumn);
  
            this.radGridView1.Rows.Add("A1", "B1", "  Ok  ");
            this.radGridView1.Rows.Add("A2", "B2", "  Ok  ");
            this.radGridView1.Rows.Add("A3", "B3", "Activate");
            this.radGridView1.Rows.Add("A4", "B4", "  Ok  ");
        }
  
        private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement.RowIndex > -1)
            {
                if ((e.CellElement.ColumnInfo).HeaderText == "Button")
                {
                    GridCommandCellElement commandCell = (GridCommandCellElement)e.CellElement;
                    RadButtonElement buttonElement = (RadButtonElement)commandCell.Children[0];
  
  
                    if (e.CellElement.Value.ToString().Trim() == "Ok")
                    {
                        buttonElement.Enabled = false;
                    }
                    else
                    {
                        buttonElement.Enabled = true;
                    }
                }
            }
  
        }
  
    }
}


This is looking at the button text and making it enabled / disabled accordingly.
I hope that helps but let me know if you have any questions
Richard
0
Paul R.
Top achievements
Rank 1
answered on 04 Mar 2011, 06:26 PM
Well your method worked & mine did not.   :)

Thanks for your time Richard.
0
Richard Slade
Top achievements
Rank 2
answered on 04 Mar 2011, 06:30 PM
Hi Paul,

Does this mean it's all sorted for you now?
Hope that helped
Richard
0
Paul R.
Top achievements
Rank 1
answered on 04 Mar 2011, 06:40 PM
It is all sorted.  Thanks again Richard.
Tags
GridView
Asked by
Paul R.
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Paul R.
Top achievements
Rank 1
Share this question
or