2

我有DataSource一个LookUpEdit. 例如,我有 2 列FirstNameLastName我想为DisplayMember这两列设置属性。我发现我应该lookUp_CustomDisplayText()像这样订阅和编辑显示文本属性:

private void lookUpCompanyPerson_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
{
     LookUpEdit edit = sender as LookUpEdit;

     if (e.DisplayText != "")
     {
           e.DisplayText = e.DisplayText + " " + (string)e.Value;          
     }            
}

但我不明白是什么e.Value,我想为所选行显示另一列,而不是所选行的 valuemember。

这就是我将数据源绑定到lookupedit的方式:

 private void populateComboBoxForCompanyPerson()
 {
     lookUpCompanyPerson.Properties.ForceInitialize();
     bs = new BindingSource(myDataSet, "CompanyPerson");            
     lookUpCompanyPerson.Properties.DataSource = bs;
     lookUpCompanyPerson.Properties.DisplayMember = "CompanyName";
     lookUpCompanyPerson.Properties.ValueMember = "PersonID";
     this.lookUpCompanyPerson.Properties.Columns.Add(new LookUpColumnInfo("PersonID"));
     this.lookUpCompanyPerson.Properties.Columns["PersonID"].Visible = false;            
     this.lookUpCompanyPerson.Properties.Columns.Add(new LookUpColumnInfo("FirstName"));
     this.lookUpCompanyPerson.Properties.Columns.Add(new LookUpColumnInfo("LastName"));
     this.lookUpCompanyPerson.Properties.Columns.Add(new LookUpColumnInfo("CompanyName"));                
 }

这就是我的数据源的样子:数据源

4

4 回答 4

4

我稍微更改了 Ian O'Brien 的代码,它可以工作:

private void lookUpCompanyPerson_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
{
      RepositoryItemLookUpEdit props;
      if (sender is LookUpEdit)
      props = (sender as LookUpEdit).Properties;
      else
      props = sender as RepositoryItemLookUpEdit;

      if (props != null && (e.Value is int))
      {
          DataRowView row = props.GetDataSourceRowByKeyValue(e.Value) as DataRowView;

          if (row != null)
          {
              e.DisplayText = String.Format("{0} {1}", row["FirstName"], row["LastName"]);

          }
      }
}
于 2012-12-07T16:32:59.810 回答
2

来自 DevExpress 文档:

  • e.Value获取或设置编辑器的当前值。
  • e.DisplayText获取或设置编辑器的显示文本

The lookup editor's value is obtained from the data source field specified by the RepositoryItemLookUpEditBase.ValueMember property. The GetDataSourceRowByKeyValue method searches for the specified value within this field and returns an object representing the first found record.

The GetDataSourceRowByKeyValue method's return value depends upon the type of the underlying data source. If the data source is a System.Data.DataTable or a System.Data.DataView, this method returns a System.Data.DataRowView object. If the data source is a custom list of items, the appropriate list item is returned.

You want to set the e.Value to the value that you want to display in the control.

private void lookUpCompanyPerson_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
{
    RepositoryItemLookUpEdit props
    if (sender is LookUpEdit)
        props = (sender as LookUpEdit).Properties;
    else
        props = sender as RepositoryItemLookUpEdit;
    if (props != null && (e.Value is int))
    {
        object row = props.GetDataSourceRowByKeyValue(e.Value);
        if (row != null)
        {
            e.Value = String.Format("{0} {1}", (DataRowView)row["FirstName"], (DataRowView)row["LastName"]);
            e.Handled = true;
        }
    }
}

Finally, here are some useful pages with more documentation:

于 2012-12-07T16:10:45.190 回答
1

我用过,就像这样;

cmb_tip.Properties.DataSource = _dt;
cmb_tip.Properties.ValueMember = "Value";
cmb_tip.Properties.DisplayMember = "Type";
cmb_tip.Properties.PopulateColumns();
cmb_tip.Properties.Columns["Value"].Visible = false;
于 2013-03-06T14:49:43.883 回答
1

这是它与版本 15.2.7 中的 LookupEditControl 和一个类的工作方式:

private void lookUpEditPatients_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
    {
        var edit = sender as LookUpEdit;
        var props = edit.Properties;
        var pat = (Patients4ComboBoxVm) props?.GetDataSourceRowByKeyValue(e.Value);
        if (pat != null)
        {
            e.DisplayText = pat.Nachname + ", " + pat.Vorname + "; " + pat.Geburtsdatum + "; " + pat.Versicherungsnummer;
        }
    }
于 2016-11-03T11:05:58.087 回答