1

我已经有了数据表。我只需要在列表视图中填写它。问题是我遇到了这个错误:

“System.Web.UI.WebControls.ListView”不包含“DisplayMember”的定义,并且找不到接受“System.Web.UI.WebControls.ListView”类型的第一个参数的扩展方法“DisplayMember”(你是缺少 using 指令或程序集引用?)

我认为列表视图有一个“DisplayMember”属性?顺便说一下,这是 ASP .NET 4.0。

namespace Eagle_Replication_Manager
{
    public partial class wfrmMain : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //on page load, I want to get a database table and populate this list view: 
            lvItems.DataSource = GetSourceDBs();

            //This does not work, error here:
            lvItems.DisplayMember = "Description";
        }

        private DataTable GetSourceDBs()
        {
            using (SqlConnection conn = new SqlConnection(AppVars.connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT [Description] FROM [Warehouse].[dbo].[Items]", conn))
                {
                    conn.Open();

                    cmd.ExecuteNonQuery();

                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        using (DataTable dt = new DataTable())
                        {
                            da.Fill(dt);
                            return dt;
                        }
                    }
                }
            }
        }
    }
}
4

2 回答 2

1

这应该有效:

    protected void Page_Load(object sender, EventArgs e)
    {
        lvItems.DataSource = GetSourceDBs();
        lvItems.DataBind();
    }
于 2012-08-29T01:58:32.120 回答
0

Public void getdataToList(object sender, System.EventArgs e) { SqlConnection conn = new SqlConnection(" << put your connection string >>");

 try {
   lvItems.Columns.Clear() ;
   lvItems.Items.Clear();

   conn.Open();
   txtSql.Text ="select * from Employee";

   SqlCommand cmd = conn.CreateCommand();
   cmd.CommandText = txtSql.Text;

   SqlDataReader dr = cmd.ExecuteReader();

   for (int i = 0; i< dr.FieldCount; i++) {
     ColumnHeader ch = new ColumnHeader();
     ch.Text=dr.GetName(i);
     lvItems.Columns.Add(ch);
   }

   ListViewItem itmX; 

   while (dr.Read()) {
     itmX=new ListViewItem(); 
     itmX.Text= dr.GetValue(0).ToString();

     for (int i=1 ; i< dr.FieldCount; i++) {
        itmX.SubItems.Add(dr.GetValue(i).ToString());
     }
     lvwResult.Items.Add(itmX);
   }
   dr.Close();
} catch ( System.Data.SqlClient.SqlException  ex) {
   Console.WriteLine("There was an error in executing the SQL." +
           "\nError Message:" + ex.Message, "SQL");
} finally {
   conn.Close();
}
于 2012-08-29T05:55:57.890 回答