Gridview RowUpdating NewValues无法保存null

时间:2013-05-23 22:09:05

标签: c# asp.net

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

entityDataSource:

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

GridView的:

<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;

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

How to set field values equal to null in RowUpdating(...)

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

0 个答案:

没有答案