zoukankan      html  css  js  c++  java
  • ASP.NET dropdownlist绑定数据却显示System.Data.DataRowView

     

    问题: 在VS中用dropdownlist控件绑定数据,浏览时却在控件里显示System.Data.DataRowView,而不是要显示的数据,代码如下:

    public static DataSet GetDataSet(DataSet ds, string tablename)
           {
               string s = "select departmentName from department_info";
               string con = ConfigurationManager.ConnectionStrings["CodematicConnectionString"].ConnectionString.ToString();
               SqlConnection conn = new SqlConnection(con);
               SqlDataAdapter adapter = new SqlDataAdapter(s, con);
               adapter.Fill(ds, tablename);
               return ds;
           }

    protected void Page_Load(object sender, EventArgs e)
           {

             if(!IsPostBack)

               {
                   DataSet ds = new DataSet();
                   string tablename = "department_Info";
                   ds = GetDataSet(ds, tablename);
                   this.ddl_post.DataSource = ds;
                          this.ddl_post.DataBind();
                   DataTable dt = ds.Tables[0];
                   DataRow dr = dt.NewRow();
                   dr[0] = "==请选择==";
                   //添加到第1行
                   dt.Rows.InsertAt(dr, 0);
                   this.ddl_post.DataSource = dt;
                   //这种方法也可以
                   //this.ddl_department.Items.Insert(0,"==请选择==");
                   //this.ddl_department.Items.FindByText("==请选择==").Selected = true;

               } 

         }

    解决:在DataBind();前加上

             ddl_post.DataTextField = "departmentName";
                  ddl_post.DataValueField = "departmentName";

    就可以了.

  • 相关阅读:
    11.【原创】Object.keys()的一般用法
    5. 【原创】table设置text-overflow: ellipsis;(超出范围显示...)不生效
    12.【转载】vscode默认常用快捷键
    13.【原创】JS读取apk安装包的信息,做应用上传
    11.【原创】chrom文件上传后,手动释放内存
    26.Mysql "truncate"与"delete"的区别
    25.【转载】Mysql timestamp类型字段的CURRENT_TIMESTAMP与ON UPDATE CURRENT_TIMESTAMP属性
    bof
    ctf Wiener tricky
    分解大素数
  • 原文地址:https://www.cnblogs.com/tangge/p/2502233.html
Copyright © 2011-2022 走看看