Click here to Skip to main content
15,907,183 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, this has been driving me mad for a few days now and I have posted on here but all the suggestions haven't seem to work so far. Below is my C# code and also parts of the XAML - if there is anymore needed I will post whatever is neeeded!!

I appreciate any help that is offered!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
using System.Collections.ObjectModel;

namespace WpfApplication4
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            _DateLabel.Content = DateTime.Today;
        }
       
        private double _height, _weight, _target;
        ObservableCollection<fooddata> _FoodDataCollection = new ObservableCollection<fooddata>();
      
        public ObservableCollection<fooddata> FoodDataCollection
        { get { return _FoodDataCollection; } }

        public class FoodData
        {
            public string Food_Name { get; set; }
            public string Calories { get; set; }
            public string Fat { get; set; }
            public string Protein { get; set; }
            public string Carbs { get; set; }
        }  

        private void calendar1_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            _DateLabel.Content = calendar1.SelectedDate;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SqlConnection conn = new SqlConnection(WpfApplication4.Properties.Settings.Default.CalorieCounterConnectionString);
            conn.Open();

            using (SqlCommand command = new SqlCommand("SELECT TOP 1 User_Name,User_Height,User_Weight,User_Target,User_Target_Date FROM [User]", conn))
            {
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    _userName.Content = reader.GetString(0);
                    _height = reader.GetDouble(1);
                    _weight = reader.GetDouble(2);
                    _target = reader.GetDouble(3);
                    _targetDate.Content = reader.GetDateTime(4);
                }
            }
            _heightLbl.Content = _height;
            _weightLbl.Content = _weight;
            _targetWeight.Content = _target;
            
            _toLose.Content = (_weight - _target);
            _caloriesLeft.Content = ((_weight - _target) * 3500);
            conn.Close();

              using (DataClasses1DataContext dc = new DataClasses1DataContext())
                 {
                     listView1.ItemsSource = dc.Foods.ToList();
                 }

        }
            
        

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            AddFood addfood = new AddFood();
            addfood.Show();
            addfood.Focus();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            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();
                
                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()

                    });
                }
                

            }
        }
  
    }
}</fooddata></fooddata></fooddata>



XAML :

<window x:class="WpfApplication4.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="925" Width="1075"  WindowStartupLocation="CenterScreen" Loaded="Window_Loaded"></window>


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


I think that is all the bits that are needed!

What I am getting back from this is no error but in the list view instead of the data it's returning - WpfApplication4.Food - this appers in each column so I know it's reading and ding something but definetly not what I want it to!

Thanks in advance for any help givin!!

Dan
Posted
Updated 5-Sep-11 7:03am
v3

1 solution

Try defining DisplayMemberBinding[^] for your gridviewcolumns.
 
Share this answer
 
Comments
DanHodgson88 5-Sep-11 13:00pm    
I had actually already done that but took it off for some silly reason while I was messing around. I have put them in and it has thrown:

XamlParseException
'Set property
'System.Windows.Controls.GridViewColumn.DisplayMemberBinding' threw an exception.' Line number '58' and line position '18'.

any ideas?? line 58 is the <gridview> line
DanHodgson88 5-Sep-11 13:05pm    
it works mate all that was me being stupid appreciate your reply pal!
DanHodgson88 5-Sep-11 13:05pm    
cdeing is frustrating at times!
Wendelius 5-Sep-11 13:24pm    
So it's working now, great :) :thumbsup:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900