我正在我的一个项目中创建动态网格。我在Page_Init
方法中添加了网格代码。通常我使用绑定列或模板列,它按预期工作。
以下是代码:
protected void Page_Init(object source, System.EventArgs e)
{
if (Session["colnames"] != null)
{
List<CommanIdTitle> CategoryIdTitle = new List<CommanIdTitle>();
MainSubCategory CateMarks = (MainSubCategory)Session["colnames"];
GridBoundColumn boundColumn_StudentId;
boundColumn_StudentId = new GridBoundColumn();
boundColumn_StudentId.DataField = "StudentId";
boundColumn_StudentId.HeaderText = "Student Id";
boundColumn_StudentId.UniqueName = "StudentId";
boundColumn_StudentId.AllowFiltering = false;
boundColumn_StudentId.Display = false;
Grid_CategoryMarks.MasterTableView.Columns.Add(boundColumn_StudentId);
GridBoundColumn boundColumn_StudentName;
boundColumn_StudentName = new GridBoundColumn();
boundColumn_StudentName.DataField = "StudnetName";
boundColumn_StudentName.HeaderText = "Student Name";
boundColumn_StudentName.UniqueName = "StudentName";
boundColumn_StudentName.AllowFiltering = false;
Grid_CategoryMarks.MasterTableView.Columns.Add(boundColumn_StudentName);
GridTemplateColumn templateColumn_AverageFinal;
templateColumn_AverageFinal = new GridTemplateColumn();
templateColumn_AverageFinal.ItemTemplate = new MyTemplate2("0");
templateColumn_AverageFinal.UniqueName = "Average";
templateColumn_AverageFinal.HeaderText = "Test";
templateColumn_AverageFinal.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
templateColumn_AverageFinal.AllowFiltering = false;
templateColumn_AverageFinal.ReadOnly = true;
Grid_CategoryMarks.MasterTableView.Columns.Add(templateColumn_AverageFinal);
}
}
但是现在新的要求是将这些列分组。所以我添加了GridGroupColumn
。
以下是新代码:
protected void Page_Init(object source, System.EventArgs e)
{
if (Session["colnames"] != null)
{
List<CommanIdTitle> CategoryIdTitle = new List<CommanIdTitle>();
MainSubCategory CateMarks = (MainSubCategory)Session["colnames"];
GridBoundColumn boundColumn_StudentId;
boundColumn_StudentId = new GridBoundColumn();
boundColumn_StudentId.DataField = "StudentId";
boundColumn_StudentId.HeaderText = "Student Id";
boundColumn_StudentId.UniqueName = "StudentId";
boundColumn_StudentId.AllowFiltering = false;
boundColumn_StudentId.Display = false;
Grid_CategoryMarks.MasterTableView.Columns.Add(boundColumn_StudentId);
GridBoundColumn boundColumn_StudentName;
boundColumn_StudentName = new GridBoundColumn();
boundColumn_StudentName.DataField = "StudnetName";
boundColumn_StudentName.HeaderText = "Student Name";
boundColumn_StudentName.UniqueName = "StudentName";
boundColumn_StudentName.AllowFiltering = false;
Grid_CategoryMarks.MasterTableView.Columns.Add(boundColumn_StudentName);
/*This code throws error*/
GridColumnGroup groupHeaderFinal = new GridColumnGroup();
groupHeaderFinal.Name = "TotalAveragePerTerm";
groupHeaderFinal.HeaderText = "Total Average Per Term";
groupHeaderFinal.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
Grid_CategoryMarks.MasterTableView.ColumnGroups.Add(groupHeaderFinal);
GridTemplateColumn templateColumn_AverageFinal;
templateColumn_AverageFinal = new GridTemplateColumn();
templateColumn_AverageFinal.ItemTemplate = new MyTemplate2("0");
templateColumn_AverageFinal.UniqueName = "Average";
templateColumn_AverageFinal.HeaderText = "";
templateColumn_AverageFinal.ColumnGroupName = "TotalAveragePerTerm";
templateColumn_AverageFinal.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
templateColumn_AverageFinal.AllowFiltering = false;
templateColumn_AverageFinal.ReadOnly = true;
Grid_CategoryMarks.MasterTableView.Columns.Add(templateColumn_AverageFinal);
}
}
当我在任何回发中添加此内容时发生错误
Sys.WebForms.PageRequestManagerServerErrorException:列名无效:
我找到的一个解决方案是设置网格EnableViewState = False
。但是,如果我执行此操作,则不会维护viewstate
并且网格崩溃并且所有数据都将丢失。
这是我的设计代码
<telerik:RadGrid ID="Grid_CategoryMarks" runat="server" EnableViewState="true"
AllowFilteringByColumn="True" AllowSorting="True"
ShowGroupPanel="false" AutoGenerateColumns="False" PageSize="10"
ShowStatusBar="true" OnNeedDataSource="Grid_CategoryMarks_NeedDataSource" OnItemDataBound="Grid_CategoryMarks_ItemDataBound"
ShowFooter="True" FilterItemStyle-HorizontalAlign="Left">
<ClientSettings AllowDragToGroup="True">
<Selecting AllowRowSelect="false" UseClientSelectColumnOnly="true"/>
<Scrolling AllowScroll="true" UseStaticHeaders="true" SaveScrollPosition="true"/>
</ClientSettings>
<MasterTableView AutoGenerateColumns="false" >
<NoRecordsTemplate>
<asp:Label ID="lbl_rec_msg" runat="server" Text="No record exist"></asp:Label>
</NoRecordsTemplate>
</MasterTableView>
</telerik:RadGrid>
答案 0 :(得分:1)
我遇到了类似的问题。问题是您已在设计中定义了网格,并在page_init
方法中再次创建了网格。
从设计中删除它并尝试从后面的代码创建。所以每次创建新的瞬间。