CommandColumn - Where can we set Button's Text

2 posts, 0 answers
  1. Majid Darab
    Majid Darab avatar
    112 posts
    Member since:
    Dec 2009

    Posted 23 Mar 2019 Link to this post

    Please take a look at this example : 

    Change the appearance of the buttons in GridViewCommandColumn

    I want to create buttons with `Delete` text of every buttons.

    But when i create a CommandColumn in GridView text of every button is empty.

    Where can we set Button's Text?

     

    Thanks in advance

  2. Hristo
    Admin
    Hristo avatar
    1520 posts

    Posted 26 Mar 2019 Link to this post

    Hi Majid,

    Please make sure that you have subscribed the grid to the CellFormatting event. You can also check my code snippet below: 
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
     
            this.radGridView1.DataSource = this.GetData();
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
     
            GridViewCommandColumn col = new GridViewCommandColumn("Command");
            this.radGridView1.Columns.Add(col);
     
            this.radGridView1.CellFormatting += this.RadGridView1_CellFormatting;
        }
     
        private void RadGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement.ColumnInfo is GridViewCommandColumn)
            {
                // This is how we get the RadButtonElement instance from the cell
                RadButtonElement button = (RadButtonElement)e.CellElement.Children[0];
                if (e.CellElement.RowInfo.Cells["Id"].Value != null)
                {
                    int id = (int)e.CellElement.RowInfo.Cells["Id"].Value;
                    if (id % 2 == 0)
                    {
                        button.Text = "Disabled";
                        button.Enabled = false;
                    }
                    else
                    {
                        button.Text = "Enabled";
                        button.Enabled = true;
                    }
                }
            }
        }
     
        private object GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Date", typeof(DateTime));
     
            for (int i = 0; i < 100; i++)
            {
                dt.Rows.Add(i, "Name " + i, DateTime.Now.AddDays(1));
            }
     
            return dt;
        }
    }

    I have also attached a screenshot showing the result. 

    Regards,
    Hristo
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top