【问题标题】:Populate dropdownlist upon gridview editmode在 gridview 编辑模式下填充下拉列表
【发布时间】:2017-11-26 08:55:20
【问题描述】:

我有一个 GridView,我从 Load 后面的代码中填充。进入编辑模式后,我需要将一列作为 DropDownList 填充另一个表中的字段。

由于 GridView 中的所有数据在非编辑视图(选择视图?)中都是正确的,我认为它与 DataTextField 或 DataValueField 有关。

网格视图:

<asp:GridView runat="server" ID="gvOFO" AllowPaging="true" PageSize="100" AllowSorting="true" AutoGenerateColumns="false" CssClass="table table-condensed text-center" DataKeyNames="OFOKey" OnRowCancelingEdit="gvOFO_RowCancelingEdit" OnRowCommand="gvOFO_RowCommand" OnRowDataBound="gvOFO_RowDataBound" OnRowDeleting="gvOFO_RowDeleting" OnRowEditing="gvOFO_RowEditing" OnRowUpdating="gvOFO_RowUpdating" OnSorting="gvOFO_Sorting" OnPageIndexChanging="gvOFO_PageIndexChanging">
    <columns>
        <templatefield><itemtemplate>column1label</itemtemplate></templatefield>
        <templatefield><itemtemplate>column2label</itemtemplate></templatefield>
        <templatefield>
            <itemtemplate>column3label</itemtemplate>
            <edititemtemplate>
                <asp:DropDownList runat="server" ID="ddlStatus" DataTextField="OFOstatus" DataValueField="OFOstatus" CssClass="form-control" />
            </edititemtemplate>
        </templatefield>
    </columns>
</gridview>

表1:

OFOKey int 身份 (PK)

日期日期时间

ReceiptPointGroupKey int(FK 到另一个表)

OFOstatus varchar(FK 到表 2)

表 2:

DayTypeKey int 身份 (PK)

DayTypeName varchar

为什么我不知道,但是这些表是由 OFOstatus 和 DayTypeName 链接的(我建议通过 int PK 链接到数据库的创建者,但无论它们如何链接,我在填充DDL)

绑定方法:

void BindGrid()
{
    using (EntityDB dc = new EntityDB())
    {
        var ds = (from ofo in dc.OFOs
                  //the join just overlays the name over the int FK of the OFO table
                  join zone in dc.ReceiptPointGroups on ofo.ReceiptPointGroupKey equals zone.ReceiptPointGroupKey
                  //-----------------------------------------------------------------
                  select new { ofo.OFOKey, ofo.Date, zone.ReceiptPointGroupName, ofo.OFOstatus }).ToList();
        gv.DataBind();
    }

【问题讨论】:

    标签: c# entity-framework linq gridview ddl


    【解决方案1】:

    就像在 RowDataBound 事件中放置链接表并填充 DropDown 的逻辑一样简单

    【讨论】:

      【解决方案2】:

      我想这与您选择的值有关: 匿名类型的ofo.OFOstatus的属性。

      你可以尝试使用命名类而不是匿名类型吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-30
        • 2011-11-21
        • 1970-01-01
        • 2011-08-31
        • 1970-01-01
        • 1970-01-01
        • 2014-10-04
        相关资源
        最近更新 更多