Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Unable to solve Datagrid solution

$
0
0

I am trying to rewrite a Windows form program to WPF. The top image is the original program that I am trying to recreate in WPF. For the moment I have a test program that I am using to try and achieve what I want.

To create the columns I use code behind : 

            foreach (DateTime dt in EachDay(dtpStartDate.SelectedDate.Value, dtpEndDate.SelectedDate.Value))
            {
                DataGridTextColumn col = new DataGridTextColumn();
                col.Header = string.Format("{0:dd/MM}", dt);
                //col.Binding = new Binding("??????");
                MyDataGrid.Columns.Add(col);
            }

        public IEnumerable<DateTime> EachDay(DateTime from, DateTime thru)
        {
            for (var day = from.Date; day.Date <= thru.Date; day = day.AddDays(1))
                yield return day;
        }

and fill the datagrid rowheader with my data from database. 

                using (MaterialEntities me = new MaterialEntities())
                {
                    var listOfItems = from i in me.items
                                      where !i.isReturned
                                      where i.categoryID == 24
                                      select new Planner
                                      {
                                          Name = i.name,
                                          ID = i.itemID
                                      };

                    MyDataGrid.ItemsSource = listOfItems.ToList();
                }

To find out which item listed on the left is reserved for a mission between the dates that the columns represent and change background colour accordingly I thought of using a multi value converter. In the conveter I will write the code needed to return the ifo from the database tables. I have got as far as using a value converter to change the background cell

    class FreightMissionConverter : IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int temp = (int)value;

            if (temp == 462)
            {
                return Brushes.Blue;
            }
            return Brushes.Yellow;
        }
}
<DataGrid x:Name="MyDataGrid" AutoGenerateColumns="False" Grid.Row="1" SelectedValuePath="ID"><DataGrid.Resources><local:FreightMissionConverter x:Key="FreightMissionConverter" /><Style TargetType="{x:Type DataGridCell}"><Setter Property="Background" Value="{Binding Path=ID, Converter={StaticResource FreightMissionConverter}}"></Setter></Style></DataGrid.Resources><DataGrid.RowHeaderStyle><Style TargetType="DataGridRowHeader"><Setter Property="Content" Value="{Binding Name}" /></Style></DataGrid.RowHeaderStyle></DataGrid>

but I will need a multi value converter to bring in the item and the date (column header). I believe that column headers aren't bindable. Perhaps there is another way round this. My other difficulty is filling the cell with the missions' number as I can only return the colour for the cell's background.  Perhaps the only solution is to run through the datagrid in code only as I did with the Winforms program? 

Thanks for any help.


:-( Still trying to program


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>