在 gridview 编辑模式下填充下拉列表

Populate dropdownlist upon gridview editmode

我有一个 GridView,它是在加载时从后面的代码中填充的。进入编辑模式后,我需要一列是一个 DropDownList,其中填充了另一个 table.

的字段

由于 GridView 中的所有数据在非编辑视图(select 视图?)中都是正确的,我认为这与 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>

Table1:

OFOKey int identity (PK)

Date DateTime

ReceiptPointGroupKey int (FK to another table)

OFOstatus varchar (FK to Table 2)

Table 2:

DayTypeKey int identity (PK)

DayTypeName varchar

我不知道这是为什么,但是 table 是由 OFOstatus 和 DayTypeName link 编辑的(我建议 link 由 int PK 编辑给创建者数据库,但不管他们如何 link 我在填充 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();
    }

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

能否尝试使用命名 class 而不是匿名类型。

就像将逻辑放入 link 表格并在 RowDataBound 事件中填充 DropDown 一样简单