How*_*ley 15 asp.net gridview dynamic
我在向GridView动态添加列时遇到问题.我需要根据DropDownList中的值更改布局 - 即包含的列.当用户更改此列表中的选择时,我需要删除除第一列之外的所有列,并根据选择动态添加其他列.
我的标记中只定义了一列 - 第0列,一个模板列,其中我声明了一个Select链接和另一个特定于应用程序的LinkButton.该专栏需要始终存在.在创建ListBoxSelection时,我删除除第一列之外的所有列,然后重新添加所需的列(在此示例中,我将其简化为始终添加"标题"列).以下是代码的一部分:
RemoveVariableColumnsFromGrid();
BoundField b = new BoundField();
b.DataField = "Title";
this.gvPrimaryListView.Columns.Add(b);
this.gvPrimaryListView.DataBind();
private void RemoveVariableColumnsFromGrid() {
int ColCount = this.gvPrimaryListView.Columns.Count;
//Leave column 0 -- our select and view template column
while (ColCount > 1) {
this.gvPrimaryListView.Columns.RemoveAt(ColCount - 1);
--ColCount;
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码第一次运行时,我看到静态列和动态添加的"标题"列.但是,下次进行选择时,第一列将生成为空(其中没有任何内容).我看到了标题栏,我看到它左边的第一列 - 但是里面没有生成任何内容.在调试器中,我可以看到gvPrimaryListView确实仍然有两列,第一列(索引0)确实是一个模板列.事实上,该列甚至保留了它的宽度,在下面的标记中设置为165px(用于调试目的).
有任何想法吗?
<asp:GridView ID="gvPrimaryListView" runat="server" Width="100%" AutoGenerateColumns="false"
DataKeyNames="Document_ID" EnableViewState="true" DataSourceID="odsPrimaryDataSource"
AllowPaging="true" AllowSorting="true" PageSize="10" OnPageIndexChanging="activeListView_PageIndexChanging"
AutoGenerateSelectButton="False" OnSelectedIndexChanged="activeListView_SelectedIndexChanged"
Visible="true" OnRowDataBound="CtlDocList_RowDataBound" Font-Size="8pt" Font-Names="Helvetica">
<Columns>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:LinkButton EnableTheming="false" ID="CtlSelectDocRowBtn" runat="server" Text="Select"
CommandName="Select" CssClass="gridbutton" OnClick="RowSelectBtn_Click" />
<asp:ImageButton EnableTheming="false" ID="DocViewBtn" runat="server" ImageUrl="../../images/ViewDoc3.png"
CssClass="gridbutton" CommandName="Select" OnClick="DocViewBtn_Click" />
</ItemTemplate>
<ItemStyle Width="165px" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:Label ID="Label6" runat="server" Text="No rows found." SkinID="LabelHeader"></asp:Label>
</EmptyDataTemplate>
</asp:GridView>
Run Code Online (Sandbox Code Playgroud)
只是一些额外的信息.
它与第一列无关,而是与它是一个TemplateField这一事实无关.如果我将一个普通列放在左侧(在标记中)并将TemplateField列向右移动,则第一列呈现正常,而(现在第二列)TemplateField列消失.
另一个奇怪的事情 - 问题不会发生在第一次回发 - 或第二次 - 但它从第三次回发开始,然后继续进行后续回发.我很难过.
我最近在gridviews中用动态列征服了silmilar问题,也许这会有所帮助.
首先关闭视图状态
第二次在oninit事件中触发的函数中以编程方式添加列
最后,我使用以下帮助程序类使得复选框在RowDataBound事件启动时实例化.是的,其中一些是硬编码的.
这里是所有代码.拥有它:) Warrenty原样,等等等等......
最后,因为我只是让我的脚湿了DotNet任何提示将不胜感激[IE不要扯我太多:)].是的'借用'来自网络的初始代码,对不起,我不记得我的头顶:(
- 在受保护的覆盖void OnInit中将其关闭
private void GridViewProject_AddColumns()
{
DataSet dsDataSet = new DataSet();
TemplateField templateField = null;
try
{
StoredProcedure sp = new StoredProcedure("ExpenseReportItemType_GetList", "INTRANETWEBDB", Context.User.Identity.Name);
dsDataSet = sp.GetDataSet();
if (sp.RC != 0 && sp.RC != 3000)
{
labelMessage.Text = sp.ErrorMessage;
}
int iIndex = 0;
int iCount = dsDataSet.Tables[0].Rows.Count;
string strCategoryID = "";
string strCategoryName = "";
iStaticColumnCount = GridViewProject.Columns.Count;
// Insert all columns immediatly to the left of the LAST column
while (iIndex < iCount)
{
strCategoryName = dsDataSet.Tables[0].Rows[iIndex]["CategoryName"].ToString();
strCategoryID = dsDataSet.Tables[0].Rows[iIndex]["CategoryID"].ToString();
templateField = new TemplateField();
templateField.HeaderTemplate = new GridViewTemplateExternal(DataControlRowType.Header, strCategoryName, strCategoryID);
templateField.ItemTemplate = new GridViewTemplateExternal(DataControlRowType.DataRow, strCategoryName, strCategoryID);
templateField.FooterTemplate = new GridViewTemplateExternal(DataControlRowType.Footer, strCategoryName, strCategoryID);
// Have to decriment iStaticColumnCount to insert dynamic columns BEFORE the edit row
GridViewProject.Columns.Insert((iIndex + (iStaticColumnCount-1)), templateField);
iIndex++;
}
iFinalColumnCount = GridViewProject.Columns.Count;
iERPEditColumnIndex = (iFinalColumnCount - 1); // iIndex is zero based, Count is not
}
catch (Exception exception)
{
labelMessage.Text = exception.Message;
}
}
Run Code Online (Sandbox Code Playgroud)
- 助手班
public class GridViewTemplateExternal : System.Web.UI.ITemplate
{
#region Fields
public DataControlRowType DataRowType;
private string strCategoryID;
private string strColumnName;
#endregion
#region Constructor
public GridViewTemplateExternal(DataControlRowType type, string ColumnName, string CategoryID)
{
DataRowType = type; // Header, DataRow,
strColumnName = ColumnName; // Header name
strCategoryID = CategoryID;
}
#endregion
#region Methods
public void InstantiateIn(System.Web.UI.Control container)
{
switch (DataRowType)
{
case DataControlRowType.Header:
// build the header for this column
Label labelHeader = new Label();
labelHeader.Text = "<b>" + strColumnName + "</b>";
// All CheckBoxes "Look Up" to the header row for this information
labelHeader.Attributes["ERICategoryID"] = strCategoryID;
labelHeader.Style["writing-mode"] = "tb-rl";
labelHeader.Style["filter"] = "flipv fliph";
container.Controls.Add(labelHeader);
break;
case DataControlRowType.DataRow:
CheckBox checkboxAllowedRow = new CheckBox();
checkboxAllowedRow.Enabled = false;
checkboxAllowedRow.DataBinding += new EventHandler(this.CheckBox_DataBinding);
container.Controls.Add(checkboxAllowedRow);
break;
case DataControlRowType.Footer:
// No data handling for the footer addition row
CheckBox checkboxAllowedFooter = new CheckBox();
container.Controls.Add(checkboxAllowedFooter);
break;
default:
break;
}
}
public void CheckBox_DataBinding(Object sender, EventArgs e)
{
CheckBox checkboxAllowed = (CheckBox)sender;// get the control that raised this event
GridViewRow row = (GridViewRow)checkboxAllowed.NamingContainer;// get the containing row
string RawValue = DataBinder.Eval(row.DataItem, strColumnName).ToString();
if (RawValue.ToUpper() == "TRUE")
{
checkboxAllowed.Checked = true;
}
else
{
checkboxAllowed.Checked = false;
}
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
我在文档中的 DataControlFieldCollection 类下找到了这个小金块。
如果您使用的是 GridView 或 DetailsView 控件,则自动创建的 DataControlField 对象(例如,当 AutoGenerateColumns 属性为 true 时)不会存储在可公开访问的字段集合中。您只能访问和操作不是自动生成的 DataControlField 对象。
我想答案是在代码中完成所有列操作,然后您的方法应该可以正常工作。
| 归档时间: |
|
| 查看次数: |
61919 次 |
| 最近记录: |