本文介绍了列表视图填充麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我无法从下面的数据库填充列表视图是我的代码,到目前为止,所有更改都将不胜感激!

Hi all I am having trouble populating a listview from a database below is my code so far any alterations would be appreciated!

DataSet dtSet = new DataSet();
         using (SqlConnection conn = new SqlConnection(WpfApplication4.Properties.Settings.Default.CalorieCounterConnectionString))
         {
             conn.Open();
             SqlCommand command = new SqlCommand("SELECT Food_Name,Food_Calories,Food_Fat,Food_Protein,Food_Carbs FROM [Food]", conn);
             SqlDataReader dr = command.ExecuteReader();
             listView1.Items.Clear();
             while (dr.Read())
             {

                 _FoodDataCollection.Add(new FoodData
                 {
                     Food_Name = dr["Food_Name"].ToString(),
                     Calories = dr["Food_Calories"].ToString(),
                     Fat = dr["Food_Fat"].ToString(),
                     Protein = dr["Food_Fat"].ToString(),
                     Carbs = dr["Food_Carbs"].ToString()

                 });
             }


         }



XAML:



XAML:

DataContext="{Binding RelativeSource={RelativeSource Self}}"







<listview height="209" horizontalalignment="Left" margin="197,321,0,0" name="listView2" verticalalignment="Top" width="304" selectionchanged="listView2_SelectionChanged" selectionmode="Single">
            <listview.view>
                <gridview>
                    <gridviewcolumn header="Name" width="120" displaymemberbinding="{Binding Food_Name}" />
                    <gridviewcolumn header="Calories" width="62" displaymemberbinding="{Binding Calories}" />
                    <gridviewcolumn header="Fat" width="30" displaymemberbinding="{Binding Fat}" />
                    <gridviewcolumn header="Protein" width="50" displaymemberbinding="{Binding Protein}" />
                    <gridviewcolumn header="Carbs" width="40" displaymemberbinding="{Binding Carbs}" />
                </gridview>
            </listview.view>
        </listview>





I have no idea what the problem is help would be appreciated!

推荐答案


这篇关于列表视图填充麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:27