为什么不用DisplayMember和ValueMember?

解决方案 »

  1.   

    谁能告诉错在哪了???  
    这两段代码没有实际的关联,是独立的!  在第一段中我是想用DataView来增加新行的  
    在第二段中我是想用DataRow来增加新行的  通过select查出的数据,我是想把它绑定在下拉列表框中显示的,  
    增加的新行并不在数据库中,也不需要保存到数据库,只是显示的时候,让它显示  
    通常在要下拉列表框中“请选择”的,我就是想在显示是多一个“请选择” 
      

  2.   

    it works for me:
    <%@ Import Namespace="System.Data.SqlClient"%>
    <%@ Import Namespace="System.Data"%>
    <form runat="server">
    <asp:DropDownList id="DropDownList1" runat="server"/><BR>
    <asp:DropDownList id="DropDownList2" runat="server" />
    </form>
    <script language="C#" runat="server">
    const string sConn = "Server=localhost;Database=pubs;UID=sa;PWD=;";
    void Page_Load(Object o, EventArgs e)
    {
      if (!IsPostBack)
      {
    SqlDataAdapter da = new SqlDataAdapter("select * from authors", sConn);
    DataSet ds = new DataSet();
    da.Fill(ds,"authors");
    da.Fill(ds,"authors2"); DataView dv = new DataView(ds.Tables["authors"]); DataRowView drv = dv.AddNew();
        drv["au_lname"] = "aaaaaaaaaaaaa";
    //drv.EndEdit(); DropDownList1.DataSource = dv;
    DropDownList1.DataTextField = "au_lname";
    DropDownList1.DataValueField = "au_id";
    DropDownList1.DataBind(); DataView dv2 = new DataView(ds.Tables["authors2"]); DataRow dr = ds.Tables["authors2"].NewRow();
    dr["au_lname"] = "bbbbbbbbbbbbbbb";
    ds.Tables["authors2"].Rows.Add(dr); DropDownList2.DataSource = dv2;
    DropDownList2.DataTextField = "au_lname";
    DropDownList2.DataValueField = "au_id";
    DropDownList2.DataBind();
      }
    }</script>