Hide a Column in Telerik RadGrid hierarchy
Solution 1
Try This:
GridBoundColumn a = new BoundColumn();
a.Display = false;
or
a.Visible = false;
Use Display in case you want to access that field otherwise use visible property
Solution 2
Please check below code.
.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)
{
}
.................
Please check the "RadGrid1_DetailTableDataBind" event in above code.
Let me know if any concern.
Solution 3
Charan if you are needing certain values from columns for calculations you should try using the DataKeyNames and and ClientDataKeyNames properties of the RadGrid, rather than binding the values to a column then hiding the column. They can be set in the RadGrid as well as the MasterTableView.
<MasterTableView DataKeyNames="id, orderId" ClientDataKeyNames="id, myId, type">
You can access the values client and server side:
(JS):
function RowSelected(sender,eventArgs)
{
var MasterTable = sender.get_masterTableView();
var row = MasterTable.get_dataItems()[eventArgs.get_itemIndexHierarchical()];
var myId = eventArgs.getDataKeyValue("myId");
}
Charan Raju C R
Updated on June 09, 2022Comments
-
Charan Raju C R almost 2 years
I have an hierarchy in Telerik RadGrid in ASP .Net as shown in below Fig. Data binding is at run time and dynamic.
My requirement is, I need to hide some columns programmatically like, "OrderID" in second level, "EmployeeID" in third level and "OrderID" in 4th level, but I need the those values for manipulation. Could you help me to achieve this ..?
-
Brian Mains over 12 yearsYou need those values on the client or server? You can hide a column, but still have access to it on the server. But if you are talking on the client, then that's a different story.
-
-
Charan Raju C R over 12 years@Not-RocketScience: In design I'm using only <telerik:RadGrid></telerik:RadGrid>. It doesn't contain any columns here. All the columns and Rows are creating at run time. So we should achieve it at run time itself using C# coding. If possible by css can you explain that..?
-
Pawel Sledzikowski over 10 yearsWorth pointing out that setting
Visible
property tofalse
is actually removing particular column fromGridColumnCollection
(the column is not being counted while accessingCount
property) andDisplay
property set tofalse
is preserve it.