列表视图子项问题 [英] List view subitems issue
问题描述
我现在想将listview数据提交到数据库,我以为我们已经找到了以下解决方案:
Hi I am trying to commit listview data to a database now I thought we had found a solution in this:
try
{
SqlConnection conn = new SqlConnection(Calorie_Counter.Properties.Settings.Default.CalorieCounterUKConnectionString);
conn.Open();
foreach (var item in listView1.Items)
{
var FoodName = item.SubItems[0].ToString(); /*PROBLEM LINE*/
using (SqlCommand command = new SqlCommand("INSERT INTO [User_Food] (FoodName,Calories,Quantity,Fat,Protein,Carbs,Date) VALUES (@FoodName,@Calories,@Quantity,@Fat,@Protein,@Carbs,@Date)", conn))
{
command.Parameters.AddWithValue("@FoodName", FoodName);
command.Parameters.Add(new SqlParameter("@Calories", Calories));
command.Parameters.Add(new SqlParameter("@Quantity", Quantity));
command.Parameters.Add(new SqlParameter("@Fat", FatContent));
command.Parameters.Add(new SqlParameter("@Protein", ProteinContent));
command.Parameters.Add(new SqlParameter("@Carbs", CarbsContent));
command.Parameters.Add(new SqlParameter("@Date", datePicker1.SelectedDate));
command.ExecuteNonQuery();
}
conn.Close();
}
}
catch(Exception ex)
{
string message = ex.Message;
MessageBox.Show(message);
}
}
由于某种原因item.SubItems [0]-不想知道它说没有子项目的定义,我知道那里是!
如果有人可以提出解决硫醇的方法,那将不胜感激!
丹
For some reason item.SubItems[0] - doesn''t want to know it says there is no definition for subitems and I knwo there Is!
If anyone could suggest asolution to thios it would be imensley appreciated!
Dan
ObservableCollection<fooddata> FoodCollection = new ObservableCollection<fooddata>();
ObservableCollection<fooddata> FoodCollection1 = new ObservableCollection<fooddata>();
ObservableCollection<fooddata> FoodCollection2 = new ObservableCollection<fooddata>();
ObservableCollection<fooddata> FoodCollection3 = new ObservableCollection<fooddata>();
public MainWindow()
{
InitializeComponent();
}
public ObservableCollection<fooddata> _FoodCollection
{ get { return FoodCollection; } }
public ObservableCollection<fooddata> _FoodCollection1
{ get { return FoodCollection1; } }
public ObservableCollection<fooddata> _FoodCollection2
{ get { return FoodCollection2; } }
public ObservableCollection<fooddata> _FoodCollection3
{ get { return FoodCollection3; } }
private void button1_Click(object sender, RoutedEventArgs e)
{
if (lunchRdo.IsChecked == true)
{
FoodCollection.Add(new FoodData
{
FoodName = foodNameTxt.Text,
Calories = Convert.ToInt16(caloriesNum.Value),
Quantity = Convert.ToInt16(quantityNum.Value),
FatContent = fatNum.Value,
ProteinContent = proteinNum.Value,
CarbsContent = carbsNum.Value
});
}
else if (breakfastRdo.IsChecked == true)
{
FoodCollection1.Add(new FoodData
{
FoodName = foodNameTxt.Text,
Calories = Convert.ToInt16(caloriesNum.Value),
Quantity = Convert.ToInt16(quantityNum.Value),
FatContent = fatNum.Value,
ProteinContent = proteinNum.Value,
CarbsContent = carbsNum.Value
});
}
else if (dinnerRdo.IsChecked == true)
{
FoodCollection2.Add(new FoodData
{
FoodName = foodNameTxt.Text,
Calories = Convert.ToInt16(caloriesNum.Value),
Quantity = Convert.ToInt16(quantityNum.Value),
FatContent = fatNum.Value,
ProteinContent = proteinNum.Value,
CarbsContent = carbsNum.Value
});
}
else if (snackRdo.IsChecked == true)
{
FoodCollection3.Add(new FoodData
{
FoodName = foodNameTxt.Text,
Calories = Convert.ToInt16(caloriesNum.Value),
Quantity = Convert.ToInt16(quantityNum.Value),
FatContent = fatNum.Value,
ProteinContent = proteinNum.Value,
CarbsContent = carbsNum.Value
});
}
}
public class FoodData
{
public string FoodName { get; set; }
public int Calories { get; set; }
public int Quantity { get; set; }
public double FatContent { get; set; }
public double ProteinContent { get; set; }
public double CarbsContent { get; set; }
}
</fooddata></fooddata></fooddata></fooddata></fooddata></fooddata></fooddata></fooddata></fooddata></fooddata></fooddata></fooddata>
在我只是想使其正常工作的那一刻,忽略任何MVVM错误,那么我将为此担心! XAML的每一列都绑定到特定的方法,即FoodName等.我不知道那个< fooddata>是什么.废话是我不能摆脱它!
Ignore any MVVM mistakes at the minute I just want to get it working then I will worry about that! The XAML has each column binded to the specific method i.e. FoodName etc.. and I have no idea what that <fooddata> crap is I can''t get rid of it!
推荐答案
检查此行,在listView1.
之后没有任何内容
Check this line, it has nothing afterlistView1.
command.Parameters.AddWithValue("@FoodName", listView1.);
而且,我要说的是,WPF没有SubItems
,只有Items
.
http://msdn.microsoft.com/en-us/library/system. windows.controls.listview.aspx [ ^ ]
http://stackoverflow.com/questions/4788925/why-does-it-incist-subitems-undefined-by-controls-listview [ http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.items.aspx [ ^ ]
Items
可以是任何类型. digimanus正确地说,如果您的item
不是ListViewItem
类型,则会出现错误.
试试这个
Moreover, I would say, WPF doesn''t have SubItems
, only Items
.
http://msdn.microsoft.com/en-us/library/system.windows.controls.listview.aspx[^]
http://stackoverflow.com/questions/4788925/why-does-it-incist-subitems-is-undefined-by-controls-listview[^]
http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.items.aspx[^]
Items
can be of any type. And digimanus rightly said that, if your item
is not of type ListViewItem
, there will be an error.
Try this
var Food = (FoodData)item;
并传递Food.FoodName
,Food.Calories
等参数.
and pass parameters as Food.FoodName
, Food.Calories
etc.
这篇关于列表视图子项问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!