代码之家  ›  专栏  ›  技术社区  ›  gruff

DevExpress级联组合框GridView数据绑定

  •  1
  • gruff  · 技术社区  · 7 年前

    我有一个 GridView 其中包含 TextField 属于 BenefitName ( string )和a ValueField 属于 BenefitInfoKeySK ( int )。

    原来我直接绑定了 datasource combobox 并且有明显的 文本字段 ValueField值字段 价值观 (代码示例2)

    我更新了代码以创建“级联”组合框 文本字段 值不再显示在我的 网格视图 但它确实显示在 comboBox 在我的编辑表单中。

    为什么 文本字段 在我的编辑表单中渲染 下拉列表框 但是 在我的 网格视图 ?

    PartialView代码示例1 (级联组合框)

        settings.Columns.Add(column =>
        {
            column.FieldName = "BenefitKey";
            column.Name = "BenefitKey";
            column.Caption = "Claim Type";
            column.Width = 200;
            column.Settings.AllowHeaderFilter = DefaultBoolean.False;
            column.EditFormSettings.Visible = DefaultBoolean.True;
            column.Settings.AllowSort = DefaultBoolean.False;
            column.EditorProperties().ComboBox(p =>
            {
                p.CallbackRouteValues = new { Controller = "BenefitClaimDetails", Action = "GetBenefitTypes", TextField = "BenefitName", ValueField = "BenefitInfoKeySK", headerEmployeeID = Model.BenefitHeaderEmployee };
                p.TextField = "BenefitName";
                p.ValueField = "BenefitInfoKeySK";
                p.ClientSideEvents.BeginCallback = "ClaimTypeComboBox_BeginCallback";
                p.EnableCallbackMode = true;
                p.Width = 200;
                p.ValidationSettings.RequiredField.IsRequired = true;
                p.ValidationSettings.RequiredField.ErrorText = "Claim Type cannot be blank";
                p.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
            });
        });
    

    PartialView代码示例2 (基本组合框)

        settings.Columns.Add(column =>
        {
            column.FieldName = "BenefitKey";
            column.Name = "BenefitKey";
            column.Caption = "Claim Type";
            column.Width = 200;
            column.Settings.AllowHeaderFilter = DefaultBoolean.False;
            column.EditFormSettings.Visible = DefaultBoolean.True;
            column.Settings.AllowSort = DefaultBoolean.False;
    
            column.ColumnType = MVCxGridViewColumnType.ComboBox;
            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            comboBoxProperties.Width = 200;
            comboBoxProperties.DataSource = repository.GetBenefitListByEmployee(Model.BenefitHeaderEmployee);
            comboBoxProperties.TextField = "BenefitName";
            comboBoxProperties.ValueField = "BenefitInfoKeySK";
            comboBoxProperties.DropDownRows = 15;
            comboBoxProperties.ValueType = typeof(int);
            comboBoxProperties.ValidationSettings.RequiredField.IsRequired = true;
            comboBoxProperties.ValidationSettings.RequiredField.ErrorText = "Claim Type cannot be blank";
            comboBoxProperties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
        });
    

    我发现了 p.TextField p.ValueField 在代码示例1中,不执行任何操作。可以删除它们而不影响代码。然而,我必须通过我的 CallBackRoute 并在控制器代码中分配:

    控制器代码

        public ActionResult GetBenefitTypes(int claimantID, string textField, string valueField, string headerEmployeeID)
        {
            return GridViewExtension.GetComboBoxCallbackResult(p => {
                p.TextField = textField;
                p.ValueField = valueField;
                p.BindList(repository.GetBenefitListByEmployee(claimantID, headerEmployeeID));
            });
        }
    

    正如您所看到的,最后这两种技术调用相同的 Repository 方法如果我能详细说明什么,请告诉我。

    编辑

    我还试图通过添加columnType值并分配数据源来修改代码示例1,如下所示。这成功显示了 文本字段 在我的 网格视图 但是 null 我认为 claimantID 阻止编辑器组合框显示任何值。因此,我还包括 JS 我分配的代码 索赔人ID

    修改的PartialView代码示例1 (工作网格视图,组合框中无值)

        settings.Columns.Add(column =>
        {
            column.FieldName = "BenefitKey";
            column.Name = "BenefitKey";
            column.Caption = "Claim Type";
            column.Width = 200;
            column.Settings.AllowHeaderFilter = DefaultBoolean.False;
            column.EditFormSettings.Visible = DefaultBoolean.True;
            column.Settings.AllowSort = DefaultBoolean.False;
            column.EditorProperties().ComboBox(p =>
            {
                p.CallbackRouteValues = new { Controller = "BenefitClaimDetails", Action = "GetBenefitTypes", TextField = "BenefitName", ValueField = "BenefitInfoKeySK", headerEmployeeID = Model.BenefitHeaderEmployee };
                p.TextField = "BenefitName";
                p.ValueField = "BenefitInfoKeySK";
                p.ClientSideEvents.BeginCallback = "ClaimTypeComboBox_BeginCallback";
                p.EnableCallbackMode = true;
                p.Width = 200;
                p.ValidationSettings.RequiredField.IsRequired = true;
                p.ValidationSettings.RequiredField.ErrorText = "Claim Type cannot be blank";
                p.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
            });
    
            column.ColumnType = MVCxGridViewColumnType.ComboBox;
            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            comboBoxProperties.DataSource = repository.GetBenefitListByEmployee(Model.BenefitHeaderEmployee, null);
            comboBoxProperties.TextField = "BenefitName";
            comboBoxProperties.ValueField = "BenefitInfoKeySK";
        });
    

    JS代码

    @*The follwing functions handle the Cascading Benefit Type combobox*@
    function OnSelectedClaimantChanged() {
        BenefitClaimDetailsGridView.GetEditor("BenefitKey").PerformCallback();
    }
    function ClaimTypeComboBox_BeginCallback(s, e) {
        e.customArgs["claimantID"] = BenefitClaimDetailsGridView.GetEditor("DependentKey").GetValue();
    }
    

    很抱歉,包含了这么多代码,而不是一个工作项目,但解决方案相当大。我希望这样就可以了。

    1 回复  |  直到 7 年前
        1
  •  0
  •   gruff    7 年前

    我找到了解决办法。

    我开始对 comboBoxProperties 逐行剖切以了解回调在什么时候开始失败,我发现由于我的声明,回调失败了: column.ColumnType = MVCxGridViewColumnType.ComboBox;

    在研究了 EditorProperties.Combobox() 在工作中我意识到在代码的那一部分, ComboBox 正在使用标准 MVCxColumnComboBoxProperties 而不是 DevExpress GridView ComboBox

    我认为问题在于,在回调中,我以一种方式声明了列类型,但随后用 comboBoxProperties 密码 我所做的改变正在改变 var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;

    var comboBoxProperties = column.PropertiesEdit as MVCxColumnComboBoxProperties;

    这里是最终确定的工作代码。对于试图自己将其组合在一起的人来说:为了实现级联组合框,这些组合框显示文本,但以不同的值存储,您将需要以下内容。

    1. 在局部视图中,给“启动器”列一个 SelectedIndexChanged 此类事件:

      settings.Columns.Add(column =>
      {
          column.FieldName = "DependentKey";
          column.Name = "DependentKey";
          column.Caption = "Claimant";
          column.Width = 300;
          column.Settings.AllowHeaderFilter = DefaultBoolean.False;
          column.EditFormSettings.Visible = DefaultBoolean.True;
          column.Settings.AllowSort = DefaultBoolean.False;
      
          column.ColumnType = MVCxGridViewColumnType.ComboBox;
          var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
          /*Get an employee model for the current employee and pass that to the Depoendents method to get their list of dependents*/
          comboBoxProperties.DataSource = repository.GetDependentDropdownList(repository.GetCurrentEmployee(employeeID: Model.BenefitHeaderEmployee).DimEmployee);
          comboBoxProperties.TextField = "DependentName";
          comboBoxProperties.ValueField = "DependentKeySK";
          comboBoxProperties.DropDownRows = 15;
          comboBoxProperties.ValueType = typeof(int);
          comboBoxProperties.ValidationSettings.RequiredField.IsRequired = true;
          comboBoxProperties.ValidationSettings.RequiredField.ErrorText = "Claimant cannot be blank";
          comboBoxProperties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
          comboBoxProperties.ClientSideEvents.SelectedIndexChanged = "OnSelectedClaimantChanged";
      });
      
    2. 在要受影响的列中,您需要这样声明(这是上面代码示例1/代码示例2的最终版本):

      settings.Columns.Add(column =>
      {
          column.FieldName = "BenefitKey";
          column.Name = "BenefitKey";
          column.Caption = "Claim Type";
          column.Width = 200;
          column.Settings.AllowHeaderFilter = DefaultBoolean.False;
          column.EditFormSettings.Visible = DefaultBoolean.True;
          column.Settings.AllowSort = DefaultBoolean.False;
          column.EditorProperties().ComboBox(p =>
          {   /*Populate the combobox with valid values based on the selected dependentKey*/
              p.CallbackRouteValues = new { Controller = "BenefitClaimDetails", Action = "GetBenefitTypes", TextField = "BenefitName", ValueField = "BenefitInfoKeySK", headerEmployeeID = Model.BenefitHeaderEmployee };
              p.ClientSideEvents.BeginCallback = "ClaimTypeComboBox_BeginCallback";
              p.Width = 200;
              p.ValidationSettings.RequiredField.IsRequired = true;
              p.ValidationSettings.RequiredField.ErrorText = "Claim Type cannot be blank";
              p.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
          });
      
          /*Display the BenefitName in the gridView. The Callback method TextField and ValueField only influence the comboBox in the Editor window*/
          var comboBoxProperties = column.PropertiesEdit as MVCxColumnComboBoxProperties;
          comboBoxProperties.Width = 200;
          comboBoxProperties.DataSource = repository.GetBenefitListByEmployee(Model.BenefitHeaderEmployee);
          comboBoxProperties.TextField = "BenefitName";
          comboBoxProperties.ValueField = "BenefitInfoKeySK";
      });
      
    3. 创建 JS 您的 Index 页码-我的代码在上面

    4. 创建 ActionResult 在您的控制器中-我的代码在上面

    有了这4个部分,您可以级联组合框,并在网格中正确显示它们