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

CommandColumn - Where can we set Button's Text

1 Answer 358 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Majid Darab
Top achievements
Rank 1
Majid Darab asked on 23 Mar 2019, 04:57 PM

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

1 Answer, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 26 Mar 2019, 01:18 PM
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.
Tags
GridView
Asked by
Majid Darab
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Share this question
or