我在ASP .Net中的Telerik RadGrid中有一个层次结构,如下图所示。数据绑定在运行时和动态。
我的要求是,我需要以编程方式隐藏一些列,如“OrderID”在第二级,“EmployeeID”在第三级和“OrderID” 在第4级,但我需要这些值进行操作。 你能帮我实现这个吗?
答案 0 :(得分:2)
请检查以下代码。
的.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”事件。
如果有任何疑虑,请告诉我。
答案 1 :(得分:2)
试试这个:
GridBoundColumn a = new BoundColumn();
a.Display = false;
或
a.Visible = false;
如果您想访问该字段,请使用“显示”,否则使用可见属性
答案 2 :(得分:1)
Charan如果您需要来自列的某些值进行计算,您应该尝试使用RadGrid的DataKeyNames和ClientDataKeyNames属性,而不是将值绑定到列然后隐藏列。它们可以在RadGrid和MasterTableView中设置。
<MasterTableView DataKeyNames="id, orderId" ClientDataKeyNames="id, myId, type">
您可以访问客户端和服务器端的值:
(JS):
function RowSelected(sender,eventArgs)
{
var MasterTable = sender.get_masterTableView();
var row = MasterTable.get_dataItems()[eventArgs.get_itemIndexHierarchical()];
var myId = eventArgs.getDataKeyValue("myId");
}
答案 3 :(得分:0)
您可以使用css隐藏列。尝试访问cell属性并将css附加到该单元格。在css中,您可以显示内容:无
希望这有帮助。