我已经有了数据表。我只需要在列表视图中填写它。问题是我遇到了这个错误:
'System.Web.UI.WebControls.ListView'不包含'DisplayMember'的定义,并且没有扩展方法'DisplayMember'接受类型'System.Web.UI.WebControls.ListView'的第一个参数' (您是否缺少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;
}
}
}
}
}
}
}
答案 0 :(得分:1)
这应该有效:
protected void Page_Load(object sender, EventArgs e)
{
lvItems.DataSource = GetSourceDBs();
lvItems.DataBind();
}
答案 1 :(得分: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();
}