如何在 RadGrid 的每一行上创建单独的 DetailTable? [英] How to create separate DetailTable on each row in a RadGrid?

查看:48
本文介绍了如何在 RadGrid 的每一行上创建单独的 DetailTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Telerik radgrid,其中的列和详细表声明如下:

I have a telerik radgrid where columns and detail tables are declared like:

<telerik:RadGrid>
  <Columns>
    <telerik:GridBoundColumn/>
    <telerik:GridBoundColumn/>
  </Columns>
  <DetailTables>
    <telerik:GridTableView
      <Columns>
        <telerik:GridBoundColumn/>
        <telerik:GridBoundColumn/>
      </Columns>
    </telerik:GridTableView
  </DetailTables>
</telerik:RadGrid>

它提供了这样的嵌套网格:

Which gives a nested grid like this:

现在,我想要的是能够以编程方式为每行指定一个详细表(那些子表).

Now, what I want is to be able to specify a detail table (those sub tables) per row, programmatically.

(我无法确定展开 fgvbvb 行时出现的嵌套表的列是否与展开 xcxcv 行时出现的列相同).

(I cannot be sure that the columns for the nested table that comes up when I expand the line fgvbvb will be the same as the columns when expanding the line xcxcv).

我已经尝试过,在 radgrid 的 OnDataBound 处理程序中没有运气(在其中我省略了 <DetailTables>)来访问嵌套表的数据结构,如下所示:

I have tried, without luck in the OnDataBound handler of the radgrid (in which I omitted <DetailTables>) to access the data structure for nested tables like this:

    protected void OnRadGridDataBound(object sender, EventArgs e)
    {
        foreach (GridDataItem item in grdActivitiesToCopy.MasterTableView.Items)
        {
            var dg = item.ChildItem.NestedTableViews[0];

        }
    }

这将过度索引数组 NestedTableViews,因为它是空的.此外,item.ChildItem.NestedTableViews 没有 setter.

This will overindex the array NestedTableViews because it is empty. Also, item.ChildItem.NestedTableViews has no setter.

如何手动将详细信息表一一填充每一行?

How do I populate each row with a detail table one by one manually?

推荐答案

首先,因为一个asp页面的生命周期.您无法访问详细信息表中的事件.

First of all , because of the life cicle of a asp page. You can't access to a event on a detail table.

如果您需要访问明细表、项目等..
您需要在 MasterTableView 中的 PreRender 中添加一个方法,如下所示:

If you need to access detail tables , items etc ..
You need to add an method to the PreRender in the MasterTableView like this:

<MasterTableView   DataSourceID="myDataSource"
                   AllowMultiColumnSorting="True" 
                   DataKeyNames="Key1,Key2,KeyN" 
                   HierarchyDefaultExpanded="True"
                   OnPreRender="Unnamed_PreRender" >

该方法将递归遍历网格.
您执行此操作的方式可能会根据您的HieararchyLoadMode而改变.

The method will recursively iterate through the grid.
The way you do it can change depending on your HieararchyLoadMode.

所以这是我的方法,如果您处于客户端或服务器绑定模式,则存在最简单的方法.

So this is my way to do it, easiest way exist if you are on Client or Serverbind mode.

遍历和加载模式由telerik 文档 .

Traversing and load mode by the telerik doc .

我很确定你不想:

手动一一用明细表填充每一行"

"populate each row with a detail table one by one manually"

您希望在您的网格中的一个子级别有多个表,并以编程方式显示一个.

You want to have Multiple table at a Sub Level in your grid and display the rigth one programmatically.

这可以通过两个简单的步骤完成:

And this is can be done in two easy step:

1/.在您的 apsx 页面中创建每个详细信息表.
请参阅此文档以获取更多信息:一个级别的几个表

1/. Create every Detail table in your apsx page.
Please refer to this documentation for more information : Several tables at a level

2/.处理显示:

protected void Unnamed_PreRender(object sender, EventArgs e)
{
  if (!IsPostBack) myControler(MASTERGRID.MasterTableView);
}

 private void myControler(GridTableView gridTableView)
{
    GridItem[] nestedViewItems = gridTableView.GetItems(GridItemType.NestedView);

    foreach (GridNestedViewItem nestedViewItem in nestedViewItems)
    {
        foreach (GridTableView nestedView in nestedViewItem.NestedTableViews)
        {

            if (nestedView.Name == "mytable12" && nestedView.Items.Count == 0)
            { HideExpandColumn(nestedView, nestedView.ParentItem["ExpandColumn"]); }

            else if (nestedView.Name == "mytable23")
            {
                if (nestedView.Items.Count == 0)// 
                    HideExpandColumn(nestedView, nestedView.ParentItem["ExpandColumn"]);
                else
                { }
            }
            if (nestedView.HasDetailTables)
                { myControler(nestedView); }
        }
    }
}

private void HideExpandColumn(GridTableView _GNVI, TableCell _cell)
{
    if (_cell.Controls.Count > 0)
    {
        _cell.Controls[0].Visible = false;
        _cell.Text = " ";
    }
    _GNVI.Visible = false;
}

您可以使用以下方法隐藏明细表:

You can hide a detail table using :

HideExpandColumn(nestedView, nestedView.ParentItem["ExpandColumn"]);

或者您可以使用控制器参数中的明细表隐藏您测试的明细表的父级:

Or you can hide the parent of the detail table you tested using the detail table that is in param of the controler :

HideExpandColumn(gridTableView, nestedView.ParentItem["ExpandColumn"]);

HideExpandColumn 会隐藏有时会留下的展开控件,即使你隐藏了明细表.

HideExpandColumn will hide the expand control that stay sometimes even if you hide th detail table.

奖励:如果您需要访问明细表中的控件.你可以使用这个:

Bonus: If you need to access to a control in a detail table. You can use this:

public static class ControlExtensions
{
public static Control FindIt(this Control control, string id)
{
    if (control == null) return null;
    Control ctrl = control.FindControl(id);
    if (ctrl == null)
    {
        foreach (Control child in control.Controls)
        {
            ctrl = FindIt(child, id);
            if (ctrl != null) break;
        }
    }
    return ctrl;
}
}

像这样在你的控制器中调用它:

Calling it in your controler like this :

else if (nestedView.Name == "DetailPV")
                {
                    if (nestedView.Items.Count == 0)
                        HideExpandColumn(gridTableView, nestedView.ParentItem["ExpandColumn"]);
                    else
                    {

                        RadLabel ctrl = (RadLabel)this.FindIt("RadLabel11");
                        ctrl.Text += "<b>" + nestedView.Items.Count.ToString() + "</b>";
                    }

这篇关于如何在 RadGrid 的每一行上创建单独的 DetailTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆