GridViewDataComboBoxColumn更新时无法获取ServiceType值求助
解决ASPxGridLookup多选值无法保存到GridView更新命令的问题
问题根源
你用自定义EditItemTemplate替换了GridViewDataComboBoxColumn的默认编辑控件,这会打断GridView自动绑定控件值到更新参数的机制;再加上ASPxGridLookup是多选模式,选中的多个值需要手动拼接成符合数据库字段要求的格式,才能传递给@ServiceType参数。
具体解决步骤
1. 监听GridView的RowUpdating事件,手动赋值参数
在后台代码中,找到你的GridView的RowUpdating事件,提取ASPxGridLookup的选中值并拼接,然后赋值给更新参数:
protected void YourGridViewID_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) { ASPxGridView grid = sender as ASPxGridView; // 从编辑行中找到ASPxGridLookup控件 ASPxGridLookup serviceTypeLookup = grid.FindEditRowCellTemplateControl(grid.Columns["ServiceType"] as GridViewDataColumn, "ddlServiceType") as ASPxGridLookup; if (serviceTypeLookup != null) { // 获取所有选中的Description值,用逗号分隔拼接 var selectedValues = serviceTypeLookup.GridView.GetSelectedFieldValues("Description"); string serviceTypeStr = string.Join(", ", selectedValues); // 更新NewValues字典,让GridView能正确传递参数 e.NewValues["ServiceType"] = serviceTypeStr; } }
2. 检查ASPxGridLookup的配置细节
- 确认
KeyFieldName="Description"是你实际要存储到数据库的字段值,且和数据库中ServiceType字段的类型匹配(比如均为字符串类型) - 多选模式
SelectionMode="Multiple"已正确设置,该配置无问题 - 数据源
serviceTypes要确保能正常加载所有可选的ServiceType选项
3. 验证数据库字段和UpdateCommand的匹配
确保数据库表中的ServiceType字段长度足够,能容纳多选拼接后的字符串(比如设置为nvarchar(255)或更长),避免截断导致值丢失。
备选方案:通过SqlDataSource的Updating事件处理
如果你的GridView绑定的是SqlDataSource,也可以直接在数据源的Updating事件中处理参数:
protected void YourSqlDataSourceID_Updating(object sender, SqlDataSourceCommandEventArgs e) { ASPxGridLookup serviceTypeLookup = YourGridViewID.FindEditRowCellTemplateControl(YourGridViewID.Columns["ServiceType"] as GridViewDataColumn, "ddlServiceType") as ASPxGridLookup; if (serviceTypeLookup != null) { string selectedStr = string.Join(", ", serviceTypeLookup.GridView.GetSelectedFieldValues("Description")); e.Command.Parameters["@ServiceType"].Value = selectedStr; } }
内容的提问来源于stack exchange,提问作者Troy Reynolds
相关产品推荐
相关产品推荐

