问题描述
晚上,全部
我一直在将数据绑定到列表视图时遇到问题.我花了很多时间尝试将数据库中的数据绑定到列表视图,但收效甚微.
这是我到目前为止的代码:
Evening all,
I have been having a bit of an issue with binding data to a listview. I have spent a fair amount of time trying to bind data from a database to a listview and I have little success.
This is the code I have so far:
private void SetListView()
{
this.ListView1.Items.Clear();
using (SqlCommand cmd = new SqlCommand("SELECT MealType.MealType, Food.FoodName, Food.Calories FROM Day INNER JOIN Diary ON Day.DiaryID = Diary.DiaryID INNER JOIN Meal ON Day.DayID = Meal.DayID INNER JOIN MealFood ON Meal.MealID = MealFood.MealID INNER JOIN Food ON MealFood.FoodID = Food.FoodID INNER JOIN MealType ON Meal.TypeID = MealType.TypeID INNER JOIN Users ON Diary.UserID = Users.UserID WHERE (Users.UserName = @username) AND MealType.MealType = @MealType", conn))
{
cmd.Parameters.Add("@username", SqlDbType.VarChar);
cmd.Parameters["@username"].Value= HttpContext.Current.Session["Username"].ToString();
cmd.Parameters.Add("@MealType", SqlDbType.VarChar);
cmd.Parameters["@MealType"].Value = "Breakfast";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
string FoodName = dr["FoodName"].ToString();
string MealType = dr["MealType"].ToString();
int Calories = Convert.ToInt32(dr["Calories"].ToString());
ListViewItem FoodItems = new ListViewItem();
FoodItems.SubItems.Add(FoodName);
FoodItems.SubItems.Add(MealType);
FoodItems.SubItems.Add(Calories);
this.ListView1.Items.Add(FoodItems);
}
}
}
}
在我看来这应该可行,但是我显然在某处出错了.我现在遇到的主要错误是ListViewItem()不是可接受的构造函数,我检查了MSDN,根据网站的默认构造函数应该没问题.
我对哪里出错了感到有些困惑.如果有人可以帮我指出我的不足,我将不胜感激.
谢谢.
Dan
To my eyes this should work but I have obviously gone wrong somewhere. The main error I am getting at the minute is that ListViewItem() is not an acceptable constructor, I checked MSDN and according to the website the default constructor should be fine.
I am just a bit confused where I have gone wrong. If anyone could give me a hand and point out my shortfall I would greatly appreciate it.
Thank you.
Dan
推荐答案
这篇关于ASP.NET ListView问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!