This is a migrated thread and some comments may be shown as answers.

Hide columns in DetailView

6 Answers 19 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 13 Jul 2014, 02:49 PM
Hello,

I am currently working on a project with a RadGrid.
The Grid has 2 levels: master and detail.

The Settings of the Grid and the DetailView are in the
Page_Load: 
RadGrid1.MasterTableView.AllowPaging = False
Dim tableViewOrders = New GridTableView(RadGrid1)
RadGrid1.MasterTableView.DetailTables.Add(tableViewOrders)
tableViewOrders.AllowPaging = True
tableViewOrders.PageSize = 10
RadGrid1.AllowSorting = True
RadGrid1.ClientSettings.Scrolling.UseStaticHeaders = True
RadGrid1.AllowFilteringByColumn = True
RadGrid1.MasterTableView.HeaderStyle.Width = Unit.Pixel(200)
RadGrid1.ClientSettings.EnableRowHoverStyle = True
RadGrid1.MasterTableView.AllowNaturalSort = False
tableViewOrders.AllowNaturalSort = False

This is the code for the event DetailTableDataBind:
Private Sub RadGrid1_DetailTableDataBind(sender As Object, e As GridDetailTableDataBindEventArgs) Handles RadGrid1.DetailTableDataBind
        'If rep.Ready_Data.Tables.Count > 1 Then
        Dim dataItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)
        Dim cellID As TableCell = dataItem("_key_id")
  
        Dim ConnectID As String = cellID.Text
        rep.Ready_Data.Tables(1).DefaultView.RowFilter = ""
        rep.Ready_Data.Tables(1).DefaultView.RowFilter = "_parent_key_id = " & ConnectID
        e.DetailTableView.DataSource = rep.Ready_Data.Tables(1).DefaultView
  
    End Sub

How can I hide some of the columns in detailView?

If I add the following lines into Page_Load:
tableViewOrders.GetColumn("_parent_key_id").Display = False
tableViewOrders.GetColumn("key_id").Display = False

It gives me the following error: 
Cannot find a column with UniqueName '_parent_key_id'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
 
Exception Details: Telerik.Web.UI.GridException: Cannot find a column with UniqueName '_parent_key_id'
 
Source Error:
Line 208:        tableViewOrders.GetColumn("_parent_key_id").Display = False
Line 209:        tableViewOrders.GetColumn("key_id").Display = False

(It seems the RadGrid doesn’t yet recognize these columns)
 
How can I hide these columns?

 
Thanks,

Daniel.

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 14 Jul 2014, 07:04 AM
Hi Daniel,

Unfortunately I couldn't replicate the issue at my end. Please have a look into the sample code snippet which works fine at my end.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" />
<asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM Customers"
    runat="server"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM Orders Where CustomerID = @CustomerID"
    runat="server">
    <SelectParameters>
        <asp:SessionParameter Name="CustomerID" SessionField="CustomerID" Type="string" />
    </SelectParameters>
</asp:SqlDataSource>

VB:
Protected Sub Page_Load(sender As Object, e As System.EventArgs)
    If Not IsPostBack Then
        RadGrid1.DataSourceID = "SqlDataSource1"
        RadGrid1.MasterTableView.DataKeyNames = New String() {"CustomerID"}
        RadGrid1.Width = Unit.Percentage(98)
        RadGrid1.PageSize = 15
        RadGrid1.AllowPaging = False
        RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric
        RadGrid1.AutoGenerateColumns = False
        RadGrid1.Skin = "Web20"
        RadGrid1.AllowSorting = True
        RadGrid1.ClientSettings.Scrolling.UseStaticHeaders = True
        RadGrid1.AllowFilteringByColumn = True
        RadGrid1.MasterTableView.HeaderStyle.Width = Unit.Pixel(200)
        RadGrid1.ClientSettings.EnableRowHoverStyle = True
        RadGrid1.MasterTableView.AllowNaturalSort = False
 
        'Add Customers table  
        RadGrid1.MasterTableView.Width = Unit.Percentage(100)
        Dim boundColumn As GridBoundColumn
        boundColumn = New GridBoundColumn()
        RadGrid1.MasterTableView.Columns.Add(boundColumn)
        boundColumn.DataField = "CustomerID"
        boundColumn.HeaderText = "CustomerID"
        boundColumn = New GridBoundColumn()
        RadGrid1.MasterTableView.Columns.Add(boundColumn)
        boundColumn.DataField = "ContactName"
        boundColumn.HeaderText = "Contact Name"
        'Add Orders table  
        Dim tableViewOrders As New GridTableView(RadGrid1)
        RadGrid1.MasterTableView.DetailTables.Add(tableViewOrders)
        tableViewOrders.AllowPaging = True
        tableViewOrders.PageSize = 10
        tableViewOrders.DataSourceID = "SqlDataSource2"
        tableViewOrders.AllowNaturalSort = False
        tableViewOrders.Width = Unit.Percentage(100)
        Dim relationFields As New GridRelationFields()
        tableViewOrders.ParentTableRelation.Add(relationFields)
        relationFields.MasterKeyField = "CustomerID"
        relationFields.DetailKeyField = "CustomerID"
        boundColumn = New GridBoundColumn()
        tableViewOrders.Columns.Add(boundColumn)
        boundColumn.DataField = "OrderID"
        boundColumn.HeaderText = "OrderID"
        boundColumn = New GridBoundColumn()
        tableViewOrders.Columns.Add(boundColumn)
        boundColumn.DataField = "OrderDate"
        boundColumn.HeaderText = "Date Ordered"
        boundColumn = New GridBoundColumn()
        tableViewOrders.Columns.Add(boundColumn)
        boundColumn.DataField = "EmployeeID"
        boundColumn.HeaderText = "EmployeeID"
        'hiding EmployeeID column
        tableViewOrders.GetColumn("EmployeeID").Display = False
    End If
End Sub

Please provide your full code if it doesn't help.
Thanks,
Shinu.
0
Daniel
Top achievements
Rank 1
answered on 14 Jul 2014, 09:12 AM
Hello Shinu

Thank you sharing the solution with me.
However, it does not work for me.

My project is different because the data in the Grid must be
dynamic (data, number of columns and column types).
Some of the data has Detail-View and some data doesn’t.
I attached a project showing the problem I have (HierarchyDifferentDataSource file).
[Demo Project

Thank you,

Daniel.
0
Shinu
Top achievements
Rank 1
answered on 15 Jul 2014, 05:44 AM
Hi Daniel,

Please try the below approach to achieve your scenario.

VB:
Protected Sub RadGrid1_PreRender(sender As Object, e As EventArgs)
    For Each column As GridColumn In RadGrid1.MasterTableView.AutoGeneratedColumns
        If column.HeaderText = "Column7" Then
            column.Display = False
        End If
    Next
End Sub

Thanks,
Shinu.
0
Daniel
Top achievements
Rank 1
answered on 15 Jul 2014, 12:08 PM
Hello Shinu 

Thank you for the solution you submitted.
 

But I need to hide column in the GridTableView not in the MasterTableView
(eg: “Column13”)
 

Thanks,

Daniel.
0
Accepted
Shinu
Top achievements
Rank 1
answered on 16 Jul 2014, 03:25 AM
Hi Daniel,

Please try the below approach to hide the DetailTable column.

VB:
Private view As GridTableView = Nothing
Protected Sub RadGrid1_DetailTableDataBind(sender As Object, e As Telerik.Web.UI.GridDetailTableDataBindEventArgs)
    view = e.DetailTableView
    Dim table As New DataTable()
    table.Columns.Add("Column11")
    table.Columns.Add("Column12")
    table.Columns.Add("Column13")
    For i As Integer = 0 To 4
        table.Rows.Add("Col11Row" + i, "Col12Row" + i, "Col13Row" + i)
    Next
    e.DetailTableView.DataSource = table
End Sub
Protected Sub RadGrid1_PreRender(sender As Object, e As EventArgs)
    If view IsNot Nothing Then
        For Each column As GridColumn In view.AutoGeneratedColumns
            If column.HeaderText = "Column13" Then
                column.Display = False
            End If
        Next
    End If
End Sub

Hope this will helps you.
Thanks,
Shinu.
0
Daniel
Top achievements
Rank 1
answered on 16 Jul 2014, 09:22 AM
Hi Shinu ,

Thank you for the solution, Its work!!

Daniel.
Tags
Grid
Asked by
Daniel
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Daniel
Top achievements
Rank 1
Share this question
or