c# - 在 Telerik RadGrid 层次结构中隐藏列

标签 c# asp.net gridview telerik telerik-grid

我在 ASP .Net 的 Telerik RadGrid 中有一个层次结构,如下图所示。数据绑定(bind)在运行时是动态的。

我的要求是,我需要以编程方式隐藏一些列,例如,第二级的 "OrderID",第三级的 "EmployeeID""OrderID" 在第 4 级,但我需要这些值进行操作。 你能帮我实现这个..? enter image description here

最佳答案

请检查下面的代码。

.aspx

 <telerik:RadGrid ID="RadGrid1" runat="server">
   </telerik:RadGrid>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:testDatabaseConnectionString %>" 
        SelectCommand="SELECT [CustomerID], [ContactName] FROM [Contacts]"></asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:testDatabaseConnectionString %>" 
        SelectCommand="SELECT [CustomerID], [OrderID], [OrderDate] FROM [Orders] WHERE ([CustomerID] = @CustomerID)">
        <SelectParameters>
            <asp:Parameter Name="CustomerID" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
        ConnectionString="<%$ ConnectionStrings:testDatabaseConnectionString %>" 
        SelectCommand="SELECT [UnitPrice], [Quantity], [OrderID] FROM [ProductInfo] WHERE ([OrderID] = @OrderID)">
        <SelectParameters>
            <asp:Parameter Name="OrderID" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

.aspx.cs

protected void Page_Init(object source, System.EventArgs e)
{
    DefineGridStructure();
}

private void DefineGridStructure()
{


    RadGrid1.ID = "RadGrid1";
    RadGrid1.DataSourceID = "SqlDataSource1";
    RadGrid1.AutoGenerateColumns = false;

    RadGrid1.MasterTableView.DataKeyNames = new string[] { "CustomerID" };

    RadGrid1.Width = Unit.Percentage(98);
    RadGrid1.PageSize = 3;
    RadGrid1.AllowPaging = true;
    RadGrid1.AllowSorting = true;
    RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
    RadGrid1.AutoGenerateColumns = false;
    RadGrid1.ShowStatusBar = true;

    RadGrid1.MasterTableView.PageSize = 3;

    //Add columns
    GridBoundColumn boundColumn;
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "CustomerID";
    boundColumn.HeaderText = "CustomerID";
    RadGrid1.MasterTableView.Columns.Add(boundColumn);

    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "ContactName";
    boundColumn.UniqueName = "ContactName";
    boundColumn.HeaderText = "Contact Name";
    RadGrid1.MasterTableView.Columns.Add(boundColumn);


    //Detail table - Orders (II in hierarchy level)
    GridTableView tableViewOrders = new GridTableView(RadGrid1);
    tableViewOrders.Name = "Child1";
    tableViewOrders.DataSourceID = "SqlDataSource2";
    tableViewOrders.Width = Unit.Percentage(100);

    tableViewOrders.DataKeyNames = new string[] { "OrderID" };

    GridRelationFields relationFields = new GridRelationFields();
    relationFields.MasterKeyField = "CustomerID";
    relationFields.DetailKeyField = "CustomerID";
    tableViewOrders.ParentTableRelation.Add(relationFields);

    RadGrid1.MasterTableView.DetailTables.Add(tableViewOrders);

    //Add columns
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "OrderID";
    boundColumn.HeaderText = "OrderID";
    tableViewOrders.Columns.Add(boundColumn);

    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "OrderDate";
    boundColumn.UniqueName = "OrderDate";
    boundColumn.HeaderText = "Date Ordered";
    tableViewOrders.Columns.Add(boundColumn);

    //Detail table Order-Details (III in hierarchy level)
    GridTableView tableViewOrderDetails = new GridTableView(RadGrid1);
    tableViewOrderDetails.Name = "Child2";
    tableViewOrderDetails.DataSourceID = "SqlDataSource3";
    tableViewOrderDetails.Width = Unit.Percentage(100);

    tableViewOrderDetails.DataKeyNames = new string[] { "OrderID" };

    GridRelationFields relationFields2 = new GridRelationFields();
    relationFields2.MasterKeyField = "OrderID";
    relationFields2.DetailKeyField = "OrderID";
    tableViewOrderDetails.ParentTableRelation.Add(relationFields2);

    tableViewOrders.DetailTables.Add(tableViewOrderDetails);

    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "UnitPrice";
    boundColumn.HeaderText = "Unit Price";
    tableViewOrderDetails.Columns.Add(boundColumn);

    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "Quantity";
    boundColumn.HeaderText = "Quantity";
    boundColumn.UniqueName = "Quantity";
    tableViewOrderDetails.Columns.Add(boundColumn);

    //Add the RadGrid instance to the controls
    RadGrid1.PreRender += new EventHandler(RadGrid1_PreRender);
    RadGrid1.DetailTableDataBind += new GridDetailTableDataBindEventHandler(RadGrid1_DetailTableDataBind);
}

void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
{
    if (e.DetailTableView.Name == "Child1")
    {
        foreach (GridColumn column in e.DetailTableView.Columns)
        {
            if (column.UniqueName == "OrderDate")
            {
                column.Visible = false;
            }
        }
    }

    if (e.DetailTableView.Name == "Child2")
    {
        foreach (GridColumn column in e.DetailTableView.Columns)
        {
            if (column.UniqueName == "Quantity")
            {
                column.Visible = false;
            }
        }
    }
}

void RadGrid1_PreRender(object sender, EventArgs e)
{

}

................................

请检查上面代码中的“RadGrid1_DetailTableDataBind”事件。

如有任何疑问,请告诉我。

关于c# - 在 Telerik RadGrid 层次结构中隐藏列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8803633/

相关文章:

c# - 将 Expression<Func<T, V>> 转换为 Expression<Func<T, Nullable<V>>>

c# - Polly 断路器模式 - 用于测试连接字符串

asp.net - Sitecore错误-找不到Xaml控件。最后一个错误是: [No Error]

gridview - Yii2 数据方法帖子在 gridView 中不起作用

c# - 将一个 child 附加到网格,设置它的行和列

c# - 系统.TypeLoadException : get_TargetFramework doesn't have an implementation

c# - mvc 5 验证过去的日期

c# - ASP.NET:多个程序集版本,相同的 Web 应用程序

c# - 尝试在 formview 中绑定(bind)下拉列表时出现空引用异常

android - 基于 ICS 的日历小部件的 Gridview