【问题标题】:DevExpress ASPxGridLookup set selected value when the key field name has multiple fieldsDevExpress ASPxGridLookup 在关键字段名称有多个字段时设置选定值
【发布时间】:2019-08-08 16:03:24
【问题描述】:

我正在使用 DevExpress ASPxGridLookup,其 KeyFieldName 具有多个字段,例如“field1;field2;field3”。我在尝试设置所选值时遇到问题。

我还有一个只有一个字段的键。因此,当我执行以下操作时,它可以在服务器端工作: lkM​​yControl.DataBind(); lkM​​yControl.Text = "SelectedKey";

但是当我尝试使用多个键对控件执行类似操作时,文本只是保持空白。我尝试使用与控件上显示的格式相同的文本来设置 Text 属性。但它只是保持空白。我还尝试了 lkMyControlWithMultipleKeyFields.GridView.Selection.SelectRowByKey("1;2;3")。但我只是得到一个例外。

【问题讨论】:

    标签: asp.net devexpress


    【解决方案1】:

    ASPxGridLookup.GridView.Selection.SelectRowByKey 方法用于在多选模式下设置选定值SSelectionMode = Multiple) .检查How to use ASPxGridLookup in multiple selection mode as the ASPxGridView editor 示例。

    但是,如果 (SelectionMode = Single),但您有一个复合 KeyFieldName,请通过 ASPxGridLookup.Text 属性以相同的方式设置一个值:

    //kMyControlWithMultipleKeyFields.GridView.Selection.SelectRowByKey("1;2;3").
    kMyControlWithMultipleKeyFields.Text = "1;2;3";
    

    或者,最好设置 ASPxGridLookup.Value 属性(如 here),因为 ASPxGridLookup.Text 需要匹配 ASPxGridLookup.TextFormatString 设置。

    【讨论】:

      【解决方案2】:

      我发现我必须做的是创建一个新字段“ID”是 ASPxGridLookup 使用的视图类中的一个字符串。在设置 QueryableSource 时,我将关键字段连接在一起,用竖线字符分隔。

      protected void dsSelecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)    
      {    
          e.KeyExpression = "ID"
          e.QueryableSource = _Model.MyTable.Select(x => new MyView()
              {
                  ID = x.KeyField1 + "|" + x.KeyField2 + "|" + x.KeyField3,
                  OtherField = x.OtherField
              });
      }
      

      在设置 ID 值时不要使用 String.Format,因为实体不知道如何将其解释为 sql。

      在设置控件的值时,我做了以下操作

      lkMyControl.DataBind();
      lkMyControl.Value = string.Format("{0}|{1}|{2}", data.KeyField1, data.KeyField2, data.KeyField3);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-27
        • 1970-01-01
        • 1970-01-01
        • 2014-09-09
        • 2014-10-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多