Telerik Radgrid:添加另一个页脚行
本文关键字:另一个 添加 Radgrid Telerik | 更新日期: 2023-09-27 18:05:33
我需要添加多个页脚行到我的RadGrid
实例;不过,现在我只想加上第二个。我目前有一个单页脚行,它的工作和显示完美,为记录。
我在Telerik论坛上发现了以下相关问题,并尝试实现它,但它不起作用:代码被执行,新的FooterItem
被添加到Controls
,但第二行只是没有出现。我需要找出原因,如果有人能帮我解决这个问题,我会很感激的。
ASP网格代码
<div id="OrderMainContent">
<telerik:RadAjaxManager runat="server" ID="RadAjaxManager1">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="RadGrid1" />
<telerik:AjaxSetting AjaxControlID="txtQuantity">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="RadGrid1" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadInputManager ID="RadInputManager1" runat="server">
<telerik:NumericTextBoxSetting BehaviorID="NumericBehavior1" Type="Number" DecimalDigits="0">
<TargetControls>
<telerik:TargetInput ControlID="RadGrid1" />
</TargetControls>
</telerik:NumericTextBoxSetting>
</telerik:RadInputManager>
<telerik:RadGrid ID="RadGrid1" runat="server" Skin="Sunset" AllowSorting="True" AutoGenerateColumns="False"
GridLines="None" ShowFooter="True" OnItemDataBound="RadGrid1_ItemDataBound" OnPreRender="RadGrid1_PreRender">
<MasterTableView DataKeyNames="ProductID" TableLayout="Fixed">
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
<Columns>
<telerik:GridBoundColumn UniqueName="colProduct" HeaderText="<%$ Resources: SiteLabels, ProductOrderForm.lblProduct %>"
HeaderStyle-HorizontalAlign="Center" DataField="ProdDesc">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn UniqueName="colQuantity" HeaderText="<%$ Resources: SiteLabels, ProductOrderForm.lblQuantity %>"
HeaderStyle-HorizontalAlign="Center" DataField="OrderQty" ColumnEditorID="txtQuantity">
<HeaderStyle Width="90" />
<ItemStyle Width="90px" />
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Width="50px" OnTextChanged="txtQuantity_TextChanged"
AutoPostBack="true">
</asp:TextBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="colPrice" HeaderText="<%$ Resources: SiteLabels, ProductOrderForm.lblBasePrice %>"
HeaderStyle-HorizontalAlign="Center" DataField="ProdUnitPrice">
<HeaderStyle Width="80px" />
<ItemStyle Width="80px" />
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%# Eval("ProdUnitPrice") %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn UniqueName="colNotes" HeaderText="<%$ Resources: SiteLabels, ProductOrderForm.lblNotes %>"
HeaderStyle-HorizontalAlign="Center">
<HeaderStyle Width="200px" />
<ItemStyle Width="200px" />
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
<ClientSettings>
<Scrolling AllowScroll="True" UseStaticHeaders="True" />
</ClientSettings>
</telerik:RadGrid>
</div>
相关代码
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
AddFooterRow(sender as RadGrid);
}
private void AddFooterRow(RadGrid grid)
{
if (grid != null)
{
GridItem[] footerItems = grid.MasterTableView.GetItems(GridItemType.Footer);
if (footerItems.Count() == 1)
{
GridTFoot foot = footerItems[0].Parent.Controls[0].Parent as GridTFoot;
for (int i = 0; i < foot.Controls.Count; i++)
{
GridFooterItem item = foot.Controls[i] as GridFooterItem;
if(item != null)
{
lastFooterPos = i;
break;
}
}
GridFooterItem existingFooter = foot.Controls[lastFooterPos] as GridFooterItem;
GridFooterItem newFooterItem = new GridFooterItem(grid.MasterTableView, 0, 0);
foreach(TableCell fc in existingFooter.Cells)
{
TableCell newFooterCell = new TableCell();
newFooterCell.Text = "allo";
newFooterItem.Cells.Add(newFooterCell);
}
foot.Controls.AddAt(lastFooterPos + 1, newFooterItem);
}
}
}
当然,如果你需要更精确的,只要问。谢谢你的帮助。
在对Telerik文档进行了一些搜索之后,我相当肯定这是不可能的。例如,在这个问题中,Telerik管理员清楚地指出"RadGrid是数据绑定控件,它的项是基于其数据源中的记录创建的,并显示数据源数据。"因此,为了在网格中添加新行,您需要在其数据源中添加新记录并重新绑定网格。"
所以,为了解决我的问题,我决定删除我的网格上的页脚,并在下面添加一个全新的网格,我将绑定到一个虚拟的DataTable
,我将在我的Page_Load
事件处理程序中创建代码。这些值要么通过Javascript在客户端确定,要么直接注入到虚拟表的行中。
如果有人找到一个更优雅的解决方案,我仍然有兴趣知道它!但现在,工作必须继续进行。
int lastFooterPos;
protected void RadGrid1_PreRender(object sender, EventArgs e) {
AddFooterRow(sender as RadGrid);
}
private void AddFooterRow(RadGrid grid) {
if (grid != null) {
GridItem[] footerItems = grid.MasterTableView.GetItems(GridItemType.Footer);
if (footerItems.Length == 1) {
GridTFoot foot = footerItems[0].Parent.Controls[0].Parent as GridTFoot;
for (int i = 0; i < foot.Controls.Count; i++) {
GridFooterItem item = foot.Controls[i] as GridFooterItem;
if (item != null) {
lastFooterPos = i;
break;
}
}
GridFooterItem existingFooter = foot.Controls[lastFooterPos] as GridFooterItem;
GridFooterItem newFooterItem = new GridFooterItem(grid.MasterTableView, 0, 0);
int k = 0;
int l = 0;
int n = 0;
int p = 0;
int a = 0;
int b = 0;
int c = 0;
foreach (TableCell fc in existingFooter.Cells) {
//decimal cost = Convert.ToDecimal(existingFooter["Marks"].Text);
TableCell newFooterCell = new TableCell();
if (k == 0) {
newFooterCell.Text = "";
newFooterCell.Height = 12;
newFooterItem.Cells.Add(newFooterCell);
k = 1;
}
else {
if (l == 0) {
newFooterCell.Text = "";
newFooterCell.Height = 12;
newFooterItem.Cells.Add(newFooterCell);
l = 1;
}
else {
if (n == 0) {
newFooterCell.Text = "";
newFooterCell.Height = 12;
newFooterItem.Cells.Add(newFooterCell);
n = 1;
}
else {
if (p == 0) {
newFooterCell.Text = "Another Total Footer:";
newFooterCell.Height = 12;
newFooterItem.Cells.Add(newFooterCell);
p = 1;
}
else {
if (a == 0) {
newFooterCell.Text = Convert.ToString(existingFooter["Marks"].Text);
newFooterCell.Height = 12;
newFooterItem.Cells.Add(newFooterCell);
a = 1;
}
else {
if (b == 0) {
newFooterCell.Text = Convert.ToString(existingFooter["Fees"].Text);
newFooterCell.Height = 12;
newFooterItem.Cells.Add(newFooterCell);
b = 1;
}
else {
if (c == 0) {
newFooterCell.Text = Convert.ToString(existingFooter["Noofstudents"].Text);
newFooterCell.Height = 12;
newFooterItem.Cells.Add(newFooterCell);
c = 1;
}
else {
newFooterCell.Text = "";
newFooterCell.Height = 12;
newFooterItem.Cells.Add(newFooterCell);
}
}
}
}
}
}
}
foot.Controls.AddAt(lastFooterPos + 1, newFooterItem);
}
}
}
}