0

我一直在努力解决这个问题。在我的 GridView 中,我试图为 EntityDataSource 中包含的列设置特定值。在我的模型中,我专门将 nullable 属性设置为 true,并且数据库中的列是 Nullable,但是当我尝试保存 null 值时,系统不会保存它。以下是我的代码片段:

实体数据源:

<asp:EntityDataSource ID="edServiceKeys" runat="server" ConnectionString="name=APIEntities" DefaultContainerName="APIEntities" EnableDelete="True" EnableFlattening="False" EnableInsert="True" EnableUpdate="True" EntitySetName="ServiceKeys"></asp:EntityDataSource>

网格视图:

<asp:GridView ID="dvServiceKeys" runat="server" AllowPaging="True" AllowSorting="True" DataKeyNames="ServiceKeyId" AutoGenerateColumns="False" DataSourceID="edServiceKeys" OnRowDataBound="dvServiceKeys_RowDataBound" OnRowUpdating="dvServiceKeys_RowUpdating">
            <Columns>

                <asp:BoundField DataField="Key" HeaderText="Key" SortExpression="Key" />
                <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                <asp:TemplateField HeaderText="CreditUnionId" SortExpression="CreditUnionId">
                    <EditItemTemplate>
                        <asp:DropDownList ID="lstCreditUnionId" AppendDataBoundItems="true" runat="server" DataSourceID="edCreditUnions" DataTextField="Credit_Union_Name" DataValueField="Credit_Union_ID">
                            <asp:ListItem Value="-1" Text="None"></asp:ListItem>
                        </asp:DropDownList>
                        <asp:HiddenField ID="hfCreditUnionId" Value='<%# Eval("CreditUnionId") %>' runat="server" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:DropDownList ID="lstRoCreditUnionId" AppendDataBoundItems="true" runat="server" DataSourceID="edCreditUnions" DataTextField="Credit_Union_Name" DataValueField="Credit_Union_ID" Enabled="False">
                            <asp:ListItem Value="-1" Text="None"></asp:ListItem>
                        </asp:DropDownList>
                        <asp:HiddenField ID="hfRoCreditUnionId" Value='<%# Eval("CreditUnionId") %>' runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Central_ID" SortExpression="Central_ID">
                    <EditItemTemplate>
                        <asp:DropDownList ID="lstCentralId" AppendDataBoundItems="true" runat="server" DataSourceID="edCentralIds" DataTextField="Central_Name" DataValueField="Central_ID">
                            <asp:ListItem Value="-1" Text="None"></asp:ListItem>
                        </asp:DropDownList>
                        <asp:HiddenField ID="hfCentralId" Value='<%# Eval("Central_ID") %>' runat="server" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:DropDownList ID="lstRoCentralId" AppendDataBoundItems="true" runat="server" DataSourceID="edCentralIds" DataTextField="Central_Name" DataValueField="Central_ID" Enabled="false">
                            <asp:ListItem Value="-1" Text="None"></asp:ListItem>
                        </asp:DropDownList>
                        <asp:HiddenField ID="hfRoCentralId" Value='<%# Eval("Central_ID") %>' runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CheckBoxField DataField="DingFree" HeaderText="DingFree" SortExpression="DingFree" />
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            </Columns>
        </asp:GridView>

听众:

protected void dvServiceKeys_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int index = dvServiceKeys.EditIndex;
        GridViewRow row = dvServiceKeys.Rows[index];
        DropDownList creditUnion = (DropDownList)row.FindControl("lstCreditUnionId");
        DropDownList central = (DropDownList)row.FindControl("lstCentralId");

        if (creditUnion.SelectedValue == "-1")
            e.NewValues["CreditUnionId"] = null;
        else
            e.NewValues["CreditUnionId"] = creditUnion.SelectedValue;

        if (central.SelectedValue == "-1")
            e.NewValues["Central_ID"] = null;
        else
            e.NewValues["Central_ID"] = central.SelectedValue;
    }

我尝试将 NewValues 设置为以下内容:

e.NewValues["Central_ID"] = null;
e.NewValues["Central_ID"] = dbNull.Value;
e.NewValues["Central_ID"] = "";
e.NewValues["Central_ID"] = string.empty;

int? nullInt = null;
e.NewValues["Central_ID"] = nullInt;

Nullable<Int32> nullInt = null;
e.NewValues["Central_ID"] = nullInt;

我可以将任何值保存到该列,但 null 除外。这是数据库中的int类型的列,还是int类型的?在代码中。此外,这是一个重复的问题

如何在 RowUpdating(...) 中设置字段值等于 null

但不幸的是,他的解决方案不是我的解决方案。我找到了其他人,但似乎每个人都找到了一个简单的解决方案,但就我而言,似乎没有一个解决方案有效。

4

0 回答 0