启用禁用允许在telerik网格的子模板上添加新行

时间:2011-09-08 18:17:53

标签: c# winforms grid telerik

是否有人知道是否可以以编程方式启用或禁用子模板中telerik网格的添加新行功能?

我有一系列行和每行的子模板。对于某些行,我希望用户能够对子模板执行操作,对于某些其他行,我不希望。

当显示网格时,我很难找到子模板的实例。

这个问题适用于winforms telerik。

1 个答案:

答案 0 :(得分:1)

这需要一点点工作,但这是可能的。我测试了以下代码,它的工作原理。希望这些评论使其不言自明:

//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;
}