0
我在我的一个项目中创建动态网格。我在Page_Init
方法中添加了网格代码。通常我会使用按预期工作的绑定列或模板列。 下面是代码:Sys.WebForms.PageRequestManagerServerErrorException:无效的列名称:
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>