四大列表控件之DropDownList控件(单选的下拉列表)

Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    学生列表:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        Width="146px">
    </asp:DropDownList>
    </form>
</body>
</html>

绑定数据到DropDownList控件

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //数据生成
                DataSet ds = new DataSet();
                ds.Tables.Add("stu");
                ds.Tables["stu"].Columns.Add("stuNo", typeof(int));
                ds.Tables["stu"].Columns.Add("stuName", typeof(string));
                ds.Tables["stu"].Columns.Add("stuScore", typeof(int));
                ds.Tables["stu"].Rows.Add(new object[] { 1, "张一", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 2, "王二", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 3, "李三", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 4, "赵四", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 5, "周五", 100 });
                //绑定数据到DropDownList控件
                this.DropDownList1.DataSource = ds.Tables["stu"];
                this.DropDownList1.DataValueField = "stuNo";
                this.DropDownList1.DataTextField = "stuName";
                this.DropDownList1.DataBind();
            }

        }

运行结果:
在这里插入图片描述

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 DataGridView 控件的 EditMode 属性设置为 EditOnEnter 或 EditOnKeystrokeOrF2 时,可以在单元格内生成下拉列表控件。 以下是在单元格内生成下拉列表控件的示例代码: 1. 在 Form_Load 事件中添加以下代码,创建一个 DataTable 并将其绑定到 DataGridView 控件: ``` Dim dt As New DataTable dt.Columns.Add("Name") dt.Columns.Add("Gender") dt.Columns.Add("Age") dt.Rows.Add("John", "Male", "25") dt.Rows.Add("Mary", "Female", "30") dt.Rows.Add("Tom", "Male", "35") DataGridView1.DataSource = dt ``` 2. 在 DataGridView1_EditingControlShowing 事件中添加以下代码,判断当前单元格是否是“Gender”列,如果是,则创建一个 ComboBox 控件并将其绑定到单元格: ``` Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing If DataGridView1.CurrentCell.ColumnIndex = 1 Then '判断当前单元格是否是“Gender”列 Dim comboBox As ComboBox = TryCast(e.Control, ComboBox) If comboBox IsNot Nothing Then comboBox.DropDownStyle = ComboBoxStyle.DropDownList '设置 ComboBox 控件下拉列表样式为 DropDownList comboBox.Items.Clear() comboBox.Items.Add("Male") comboBox.Items.Add("Female") End If End If End Sub ``` 注意:在 DataGridView1_EditingControlShowing 事件中创建 ComboBox 控件时,要将其下拉列表样式设置为 DropDownList,这样用户就只能从下拉列表中选择一个值。 完成上述操作后,在单元格内单击并进入编辑模式时,就会出现一个下拉列表控件。用户可以从下拉列表中选择一个值,然后按 Enter 键或单击其他单元格来保存所选值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值