zoukankan      html  css  js  c++  java
  • 有一个无效 SelectedValue,因为它不在项目列表中

    有一个无效 SelectedValue,因为它不在项目列表中

    这个错误让我头疼了很长时间,在网上也查找了很多方法,但对我来说好像不奏效。

    找来找去,最后还是决定用笨方法来解决。


      protected void XX_DataBinding(object sender, EventArgs e)
        {

            DropDownList ddl = (DropDownList)sender;

            ddl.Items.Add("");

            CreateDDL("select dataValue from daiBasicData where dataClass='XX' ", ddl);

          
            DataRowView  dr = (DataRowView)DetailsView1.DataItem;
            int i = ddl.Items.IndexOf(ddl.Items.FindByValue(dr.Row["aField"].ToString()));
            if (i == -1) i = 0;
            ddl.SelectedValue = ddl.Items[i].Value;

        }

       private void CreateDDL(String sql, DropDownList ddl)
        {
            try
            {
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        SqlCommand comd = new SqlCommand();
                        comd.Connection = conn;
                        comd.CommandText = sql;
                        SqlDataReader reader = comd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                if (reader[0] != System.DBNull.Value)
                                    ddl.Items.Add(reader[0].ToString());
                            }
                        }
                        reader.Close();
                        comd.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

  • 相关阅读:
    Codeforces Round #719 (Div. 3) 题解
    Codeforces Global Round 14 A~F题解
    AtCoder Beginner Contest 199 题解
    Codeforces Round #716 (Div. 2) A~D 题解
    Codeforces Round #713 (Div. 3) 题解
    Codeforces Round #712 (Div. 2) A~E 题解
    CodeCraft-21 and Codeforces Round #711 (Div. 2) A~E 题解
    CF839 D 莫比乌斯反演
    java存大数和高精度浮点数(BigInteger与BigDecimal)
    java科学计算常用方法(Math)
  • 原文地址:https://www.cnblogs.com/liuzhengdao/p/1282769.html
Copyright © 2011-2022 走看看