asp.net - 向 ASP.NET Gridview 添加动态列

标签 asp.net gridview dynamic

我在向 GridView 动态添加列时遇到问题。我需要根据 DropDownList 中的值更改布局——即包含的列。当用户更改此列表中的选择时,我需要删除除第一列之外的所有列,并根据选择动态添加其他列。

我的标记中只定义了一个列——第 0 列,一个模板列,我在其中声明了一个 Select 链接和另一个特定于应用程序的 LinkBut​​ton。该列需要始终存在。进行 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;
    }
}

这段代码第一次运行时,我看到了静态列和动态添加的“标题”列。但是,下次进行选择时,第一列将生成为空(其中没有任何内容)。我看到了标题栏,我看到了它左边的第一栏——但是里面没有生成任何东西。在调试器中,我可以看到 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>

只是一些额外的信息。

它与它是第一列的事实无关,而与它是一个 TemplateField 的事实有关。如果我在左侧(在标记中)放置一个普通列并将 TemplateField 列向右移动,则第一列呈现良好,并且(现在是第二个)TemplateField 列消失。

另一件奇怪的事情——问题不会发生在第一次回发——或第二次——但它从第三次回发开始,然后继续进行后续的回发。我难住了。

最佳答案

我最近在 gridviews 中解决了动态列的类似问题,也许这会有所帮助。

首先关闭 View 状态
其次,在 oninit 事件中触发的函数中以编程方式添加列
最后,当 RowDataBound 事件开始时,我使用以下帮助器类使复选框实例化。是的,其中一些是硬编码的。

哎呀这是所有的代码。有它:) Warrenty 按原样,等等等等...

最后,因为我刚刚接触 DotNet,任何提示都将不胜感激[IE 不要扯我太多:)]。是的,从网上某处“借用”了初始代码,抱歉,我不记得了:(

-- 在 protected 覆盖中将其关闭 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;
        }
    }

-- 助手类
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
}

关于asp.net - 向 ASP.NET Gridview 添加动态列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/234935/

相关文章:

Java 动态代理 - 如何引用具体类

c# - 为什么我的 ClaimsIdentity IsAuthenticated 总是错误的(对于 web api 授权过滤器)?

需要 PHP 网格建议

.net - 如何隐藏 WPF ListView 的 header ?

android - 捕获图像并动态添加到 GridView

javascript - Jquery:包装源自 API 的动态创建的内容

c - 在结构指针中访问动态内存

ASP.NET MVC - 使用母版页查看,如何设置标题?

asp.net - 在vb.net中,为什么读取mysql查询结果时出错?

c# - ORA-01006 : bind variable does not exist ASP. 网络