ttbaojian
标题】向DataTable中添加自定义行

内容

  向DataTable中添加自定义行,然后绑定到DropDownList控件中,并且默认行为控件的第一行

  如果表结构如下:
                    字段名称        类型 
                      kw_id             int
                      kw_name        nvarchar(32)

    假设已经获得了DataSet ds 

方法一:

            //根据现在表中的行,创建新行(自己定义的行)
            DataRow dr =ds.Table[0].NewRow();
            
            
dr["kw_name"] = "请选择关键词";

            dr["kw_id"] = 0;
            ds.Tables[0].Rows.InsertAt(dr,0); //在表中插入行
       

        //绑定

        this.ddlKeyword.DataSource = ds.Tables[0].DefaultView;

        this.ddlKeyword.DataTextField = "kw_name";

        this.ddlKeyword.DataValueField = "kw_id";

        this.ddlKeyword.DataBind();

方法二:
        //先绑定数据,再向DropDownList插入元素
        this.ddlKeyword.DataSource = ds.Tables[0].DefaultView;

        this.ddlKeyword.DataTextField = "kw_name";

        this.ddlKeyword.DataValueField = "kw_id";

        this.ddlKeyword.DataBind();

  
        ListItem firstItem=new ListItem("请选择关键词","0");
                ddlKeyword.Items.Insert(0,firstItem);
                this.ddlKeyword.SelectedIndex = 0;


【关键词】
向DataTable中插入自定义行 向DataTable中插入空白行 定制DropDownList控件 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-23
  • 2021-08-22
  • 2022-12-23
  • 2021-10-16
  • 2021-11-18
猜你喜欢
  • 2022-12-23
  • 2021-06-30
  • 2022-02-23
  • 2022-12-23
  • 2021-08-28
相关资源
相似解决方案