动态生成的Telerik MVC3网格 - 添加复选框

时间:2012-03-21 17:35:18

标签: partial-views dynamic-data telerik-grid telerik-mvc

我有一个根据搜索条件动态生成的网格。我使用Ajax在局部视图中渲染网格。一切正常。

我现在需要添加一个复选框列作为第一列。

此外,如何进行过滤,排序分页等工作,因为它处于局部视图中。 当我点击标题进行排序时,我得到一个页面未找到错误,过滤器图标没有做任何事情。

还有一件事。当我尝试将GridCommandColumnSettings添加到网格时,我得到错误 "无效的初始化成员声明符"

以下是gridcolumnsettings的代码

    public GridColumnSettings[] NewColumns(DataTable fullDT)
    {
        GridColumnSettings[] newColumns = new GridColumnSettings[fullDT.Columns.Count];

        for (int i = 0; i < fullDT.Columns.Count; i++)
        {
            // set the visibility property for the DeliveryID
            bool boolDeliveryID;
            if (fullDT.Columns[i].ColumnName == "DeliveryID")
                boolDeliveryID = false;
            else
                boolDeliveryID = true;

            newColumns[i] = new GridColumnSettings
            {
                new GridCommandColumnSettings
                {
                    Commands = 
                    {
                        new GridEditActionCommand(),
                        new GridDeleteActionCommand()
                    },
                    Width = "200px",
                    Title = "Commands"
                },
                Member = fullDT.Columns[i].ColumnName,
                Title = fullDT.Columns[i].ColumnName,
                Visible = boolDeliveryID,
                Filterable = true,
                Sortable = true
            };
        }
        return newColumns;
    }

任何建议都将不胜感激。

由于

我编辑了我的帖子,为网格添加我的部分

Here is my partial for the grid

@(Html.Telerik().Grid<System.Data.DataRow>(Model.Data.Rows.Cast<System.Data.DataRow>())
.Name("Grid")
.Columns(columns =>
{
    columns.LoadSettings(Model.Columns as IEnumerable<GridColumnSettings>);
})
.DataBinding(dataBinding =>       dataBinding.Ajax().Select("_DeliveryManagerCustomBinding", "Deliveries"))
.EnableCustomBinding(true)
 .Resizable(resize => resize.Columns(true))

1 个答案:

答案 0 :(得分:1)

当我使用Telerik Grid控件时,我不会以这种方式添加列,但是看看你正在做什么我会猜测你需要做类似以下的事情:

将newColumns数组的大小增加1(因为我们要在复选框列中添加):

GridColumnSettings[] newColumns = new GridColumnSettings[fullDT.Columns.Count + 1];

如果您想在开头使用它,则需要在for循环之前执行以下操作:

GridColumnSettings s = new GridColumnSettings() {
  ClientTemplate("<input type=\"checkbox\"  name=\"checkeditems\" value=\"some value\" />")
  Title("title goes in here")
};

然后你将它添加到你的数组中:

newColumns[0] = s;

然后将for循环的起始索引增加到1:

for (int i = 1; i < fullDT.Columns.Count; i++)

复选框列将在开头