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

如何使下拉列表默认为表中的第一行名称

  •  2
  • Scott  · 技术社区  · 15 年前

    编辑:更多代码

    Dim oControl As New Forms.Control()
    
                                Using types As New DataSet
                                    With oDal
                                        .Parameters.Clear()
                                        .Execute(sql, types)
                                    End With
                                    With ddlType
                                        .DataSource = types.Tables(0)
                                        .DataTextField = "Name"
                                        .DataValueField = "TypeId"
                                        .Items.Clear()
                                        .DataBind()
                                        If .Items.Count > 0 Then
                                            .SelectedIndex = 0
                                        End If
                                    End With
    
                                End Using
                                'set queue type with our asp:dropdownlist control
                                oControl = New Forms.Control(Forms.Control.ControlType.ComboBox)
                                'we need to connect the two controls
                                oControl.Id = ddlType.ID
    
                                'add it to the grid
                                With .Columns.Add("CredentialTypeId", "Type", 85)
                                    .Editor = oControl
                                End With
    

    出于某种原因,当您单击下拉列表时,“0”将显示为默认值。它需要是表中的第一个文本/值对。表目前只返回一行,这是正确的,但0仍然是默认值。

    有人知道为什么吗?

    2 回复  |  直到 15 年前
        1
  •  1
  •   Bill    15 年前
    With ddlType
        .DataSource = types.Tables(0)
        .DataTextField = "Name"
        .DataValueField = "TypeId"
        .DataBind()
        If .Items.Count > 0 Then
          .SelectedIndex = 0
        End If
    End With
    

    编辑 :
    鉴于你的结果和评论,我认为 类型 表为空或代码未运行。尝试添加一些断点并测试一些值。

    types.Tables(0) isnot nothing
    types.Tables(0).Rows.Count
    

    编辑2 :
    看看你的代码,我发现了一个不同的问题。你永远不会满足于控制。我不知道为什么要创建一个全新的控件,并将其ID设置为ddltype的ID。为什么不直接使用ocontrol进行数据绑定?或者将ddltype附加到网格?

        2
  •  0
  •   VoltaicShock    15 年前

    我知道这可能行不通,但还是要说出来。

    绑定数据之前,是否尝试清除所有项?

    ddType.Items.Clear();