Hey guys, check out this quick tip on how to disable command buttons like the Insert, Edit and Delete in the ASPxGridView.
The R&D team was also inspired to create a new event in the ASPxGridView to simply the approach described above. But more on that later.
Scenario
You've enabled edit/insert/delete functionality for your grid, however, you need to disable it for certain rows.
Solution
We'll dynamically check which rows need disabling because only certain rows will be disabled. The HtmlCommandCellPrepared event is perfect for this task because it allows you to change the settings of individual command column cells. For example, this code below will hide the 'New' button and disable the selection check box for every other row:
protected void ASPxGridView1_HtmlCommandCellPrepared(object sender, ASPxGridViewTableCommandCellEventArgs e)
{
if (e.CommandCellType == DevExpress.Web.ASPxGridView.GridViewTableCommandCellType.Data)
{
if (e.VisibleIndex % 2 == 0)
{ // odd row
e.Cell.Controls[1].Visible = false; // hide the New button
((WebControl)e.Cell.Controls[3]).Attributes["disabled"] = "true"; // disable the selection checkbox
}
}
}
You can download and see a live demo of this code at Code Central: How to customize command buttons in individual rows
New Event For Easier Approach
While the approach described above is good, the R&D team wanted to improve access to the command buttons at runtime. So a new event was implemented for the upcoming DXperience 2009 Volume 1 release. The new CommandButtonInitialize event allows you to change command button controls visibility and also enable/disable them easily. For example, the code below shows how to hide every third "Select" button and disable every second 'Select" button:
protected void ASPxGridView1_CommandButtonInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCommandButtonEventArgs e)
{
if (e.Button.ButtonType != DevExpress.Web.ASPxGridView.ColumnCommandButtonType.Select) return;
e.Visible = e.VisibleIndex % 3 != 1;
e.Enabled = e.VisibleIndex % 2 != 1;
}