You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASPxGridView层级网格中DropDownList选中值无法插入SQL数据库排查

问题排查与解决方案

我来帮你梳理下这个DropDownList获取为Null的问题,咱们一步步来解决:

核心问题分析

你遇到的FindEditRowCellTemplateControl返回Null,主要是因为模板控件的获取方式有误,加上一些细节没处理到位,我把具体原因和修正步骤列出来:


1. 修正控件获取逻辑

在RowInserting事件里,直接用dgDepots引用网格可能会出问题,应该用触发事件的sender来获取当前网格实例,同时要确保正确定位到列和模板控件:

修改后的dgDepots_RowInserting方法:

protected void dgDepots_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
{
    e.Cancel = false;
    // 获取当前触发事件的GridView实例
    ASPxGridView currentGrid = sender as ASPxGridView;

    // 逐个获取EditItemTemplate里的DropDownList
    GridViewDataColumn locationCol = currentGrid.Columns["LocationCode"] as GridViewDataColumn;
    DropDownList ddlLocation = currentGrid.FindEditRowCellTemplateControl(locationCol, "ddlLocation") as DropDownList;

    GridViewDataColumn costCenterCol = currentGrid.Columns["CostCenterCode"] as GridViewDataColumn;
    DropDownList ddlCostCenter = currentGrid.FindEditRowCellTemplateControl(costCenterCol, "ddlCostCenterCode") as DropDownList;

    GridViewDataColumn glCol = currentGrid.Columns["GLCode"] as GridViewDataColumn;
    DropDownList ddlGL = currentGrid.FindEditRowCellTemplateControl(glCol, "ddlGLCode") as DropDownList;

    // 先检查控件是否获取成功(调试用)
    if (ddlLocation == null || ddlCostCenter == null || ddlGL == null)
    {
        // 可以在这里加调试输出,比如Response.Write("某个下拉框未找到");
        return;
    }

    // 执行插入操作,注意用using确保连接释放,同时指定存储过程类型
    using (SqlConnection con = new SqlConnection(你的数据库连接字符串))
    {
        SqlCommand cmd = new SqlCommand("SP_Insert_Line_Item", con);
        cmd.CommandType = CommandType.StoredProcedure; // 一定要加这个!

        cmd.Parameters.AddWithValue("@LocationCode", ddlLocation.SelectedValue);
        cmd.Parameters.AddWithValue("@LocationName", ddlLocation.SelectedItem.Text);
        cmd.Parameters.AddWithValue("@GLCode", ddlGL.SelectedValue);
        cmd.Parameters.AddWithValue("@CostCenterCode", ddlCostCenter.SelectedValue);

        con.Open();
        int i = cmd.ExecuteNonQuery();
        con.Close();
    }

    // 插入完成后刷新网格并退出编辑模式
    currentGrid.CancelEdit();
    currentGrid.DataBind();
}

2. 确保下拉框正确绑定到数据

你提到已经成功绑定下拉列表,但建议把绑定逻辑移到CellEditorInitialize事件里,这样在编辑/插入模式下都能确保控件有数据:

首先在ASPX的dgDepots标签里添加事件:

<dx:ASPxGridView ID="dgDepots" runat="server" ... OnCellEditorInitialize="dgDepots_CellEditorInitialize">

然后在CS里实现这个事件:

protected void dgDepots_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridViewEditorEventArgs e)
{
    ASPxGridView grid = sender as ASPxGridView;
    // 只在编辑或插入模式下处理
    if (grid.IsNewRowEditing || grid.IsEditing)
    {
        // 绑定LocationCode下拉框
        if (e.Column.FieldName == "LocationCode")
        {
            DropDownList ddl = e.Editor as DropDownList;
            // 这里替换成你的Web服务绑定逻辑
            // ddl.DataSource = 你的Web服务返回的数据源;
            // ddl.DataTextField = "显示文本字段名";
            // ddl.DataValueField = "值字段名";
            // ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("Select", "Select")); // 添加默认选项
        }
        // 绑定CostCenterCode下拉框
        else if (e.Column.FieldName == "CostCenterCode")
        {
            DropDownList ddl = e.Editor as DropDownList;
            // 你的Web服务绑定逻辑
        }
        // 绑定GLCode下拉框
        else if (e.Column.FieldName == "GLCode")
        {
            DropDownList ddl = e.Editor as DropDownList;
            // 你的Web服务绑定逻辑
        }
    }
}

3. 其他需要检查的细节

  • 确认ASPX里的DropDownListID和CS代码里的完全一致(比如ddlLocation有没有拼写错误);
  • 确保列的FieldName和代码里引用的完全匹配(比如LocationCode大小写是否一致);
  • 原来代码里的Location.SelectedItem.Text这类直接引用控件ID的写法要删掉,模板里的控件必须通过GridView的方法获取,不能直接访问。

内容的提问来源于stack exchange,提问作者sa f

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 16:42:43