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

C#ASP.NET-填充组合框以显示数据源中的两个值?

  •  2
  • scrot  · 技术社区  · 17 年前

    因此,我不想简单地用“rowid”(可能的值“1”、“2”、“3”等)填充它,我希望它也显示“letter”(“a”、“b”、“c”)列,但在同一个框中。

    它发送的实际值只能是rowid值,但出于用户友好的目的,我需要这两个值。

    this.comboBox1.DataSource = this.tblIDSourceBindingSource;
    this.comboBox1.DisplayMember = "rowid";
    this.comboBox1.FormattingEnabled = true;
    this.comboBox1.Name = "comboBox1";
    this.comboBox1.ValueMember = "rowid";
    

    4 回复  |  直到 17 年前
        1
  •  3
  •   Mesh    17 年前

        // FormatString property is shown in the designer, so you can provide
        // some designer support. If you are not using the desinger then you
        // either set it in the form/control ctor, or use a string directly
        // in the Format event.
        ListControl.FormatString = "{0}/{1}" ;
    
        private void listBoxCategory_Format(object sender, ListControlConvertEventArgs e)
            {
                e.Value = string.Format(((ListBox)sender).FormatString,
                    ((ClassOfListItem)e.ListItem).LongName,
                    ((ClassOfListItem)e.ListItem).Key);
        }
    
        2
  •  2
  •   Andy_Vulhop    17 年前

    假设:你不能只显示“字母”就完成它。

    select rowid, letter, blah
    from table
    

    做些像

    select rowid + ' ' + letter as display, rowid, letter, blah
    from table
    

    或者,如果您不喜欢空格,请使用您选择的分隔符(管道、斜杠、圆括号中的rowid等)。

    然后使用“display”作为DisplayMember。

    p、 如果rowid是int,则需要使用CAST或CONVERT使其成为nvarchar或varchar。

    p、 如果不能更改proc,那么很长的路是创建一个与返回相同的light实体,但是添加一个名为Display的只读属性,其get类似于

    return string.Format("{0}{1}{2}", this.rowid, delimiterOfChoice, this.letter);
    

    或者类似的东西。

        3
  •  0
  •   eviltobz    17 年前

        4
  •  0
  •   Alex    14 年前

    您应该向类/页添加一个新属性,该属性接受数据源中两个字段的值,并将displaymember分配给该属性

    推荐文章