海纳百川,有容乃大
善于总结,积累软财富
标题】向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控件 

posted on 2007-09-05 19:48  海纳百川  阅读(924)  评论(0编辑  收藏  举报

首页原创.NET区 div1
.NET新手区 div2
精华区 div3
专家区 div4
读书心得区 div5
百度主题实验室
百度主题推广 div7

-->