Erion
Erion

Reputation: 654

enable disable the allow add new row on a child template of a telerik grid

Does anyone know if it is possible to programatically enable or disable the add new row feature of the telerik grid in a child template ?

I have a series of rows and a child template for each row. For some rows I want the user to be able to perform operations on the child template, for some other rows i don't.

I am having a hard time finding the instance of the child template when the grid get's displayed.

This question is for winforms telerik.

Upvotes: 2

Views: 1381

Answers (1)

Stephen McDaniel
Stephen McDaniel

Reputation: 2968

It takes a little work but this is possible. I tested the following code and it works. Hopefully the comments make it self-explanatory:

//Attach an event handler for CreateRowInfo on the child template that you want
//   to control the Add Row feature in Form_Load or somewhere.
//If you just have one template, you could use radGridView1.MasterTemplate.Templates[0]
template.CreateRowInfo += new GridViewCreateRowInfoEventHandler(template_CreateRowInfo);

private void template_CreateRowInfo(object sender, GridViewCreateRowInfoEventArgs e)
{
    //If we aren't dealing with the New Row, ignore it
    if (!(e.RowInfo is GridViewNewRowInfo))
        return;

    //Grab our parent's info (we need the parent because the parent is the
    //   one that has the actual data.  The new row isn't bound to anything
    //   so it doesn't really help us.)
    var parentInfo = (GridViewHierarchyRowInfo) e.RowInfo.Parent;

    //Make sure the parentInfo isn't null.  This method seems to be called
    //   more than once - and some of those times the parent is null
    if (parentInfo == null)
        return;

    //We can now grab the actual data for this row.  In this case, the grid
    //   is bound to a bunch of Employees
    var rowData = (Employee)parentInfo.DataBoundItem; 

    //Do some test on the data to figure out if we want to disable add
    if(rowData.Name == "Can't Add On Me")
        //If so, we make this row (the 'New Row' row) not visible
        e.RowInfo.IsVisible = false;
}

Upvotes: 1

Related Questions