-1

i recently ran into and Error looking like this:

"unable to cast object of type pPP_2.Module to type System.Data.DataRowView".

I have a Datagrid inside my Code which has a Button column. Look like this:

enter image description here

Xaml Code behind:

<DataGrid x:Name="ModuleGrid" Grid.ColumnSpan="6" Grid.Column="1" Grid.RowSpan="4" 
                  Grid.Row="1" AutoGenerateColumns="False" CanUserAddRows="False" Margin="10,10,10,13" GridLinesVisibility="None" AlternatingRowBackground="DarkGray">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Module Name" Binding="{Binding Path=Name}"></DataGridTextColumn>
                <DataGridTemplateColumn Header="Features">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ListView ItemsSource="{Binding Features}" SelectedItem="{Binding Feature}" >
                                <ListView.DisplayMemberPath>
                                    Name
                                </ListView.DisplayMemberPath>
                            </ListView>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Min:Values">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ListView ItemsSource="{Binding Features}" SelectedItem="{Binding Feature}" >
                                <ListView.DisplayMemberPath>
                                    MinValue
                                </ListView.DisplayMemberPath>
                            </ListView>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Max:Values">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ListView ItemsSource="{Binding Features}" SelectedItem="{Binding Feature}" >
                                <ListView.DisplayMemberPath>
                                    MaxValue
                                </ListView.DisplayMemberPath>
                            </ListView>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Change Per Minute Per Liter">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ListView ItemsSource="{Binding Features}" SelectedItem="{Binding Feature}" >
                                <ListView.DisplayMemberPath>
                                    ChangePerMinutePerLiter
                                </ListView.DisplayMemberPath>
                            </ListView>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Click="ButtonBase_OnClick" Background="LightSkyBlue" Margin="5">Pass</Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

Each Row is filled with a custom Object of Type Module, Code behind:

public class Module
    {
        public string Name { get; set; }
        public string UniqueName { get; set; }
        public Dictionary<Property, double> Properties { get; set; }
        public List<Feature> Features { get; set; }

        private Dictionary<Material, double> content = new Dictionary<Material, double>();
        public Dictionary<Material, double> Content
        {
            get
            {
                return content;
            }
            set
            {
                content = value;
            }
        }
        public double FillingCapacity { get; set; }
        public double FillingAmount
        {
            get
            {
                double ret = 0;
                foreach (double a in Content.Values)
                {
                    ret += a;
                }
                return ret;
            }
        }

        public override string ToString()
        {
            return Name;
        }
    }
public class Feature
    {
        public string Name { get; set; }
        public string FeatureType { get; set; }
        public Property UsedProperty { get; set; }
        public double MinValue { get; set; }
        public double MaxValue { get; set; }
        public double ChangePerMinutePerLiter { get; set; }
    }

)

And now the Code for the Button Click Event: (of course the Properties are declared at the beginning of the Class but i show them here to make sure the connection is clear)

public String ChosenModule { get; set; }

public List<String> ChosenMods { get; set; }

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            try
            {
                ---DataRowView dataRowView = (DataRowView)((Button)e.Source).DataContext;---
                ChosenModule = dataRowView[0].ToString();
                ChosenMods.Add(ChosenModule);
                //This is the code which will show the button click row data. Thank you.
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

So now to my problem: The ButtonBase_OnClick method should pass the module of the chosen line to a list of Strings ChosenMods(i get the Name of a Module which is declared a String with the datarowview[0]). When i Click the button i get the Exception:

Unable to cast object of type "pPP_2.Module" to type "System.Data.DataRowView" in the Line which i marked with the ---.

I searched so much already but i am not able to find a solution for my problem.. Thanks to reviewing my problem,

Greeting, IRezzet.

IRezzet
  • 13
  • 9
  • Why just don't use `Feature` property, which is bound to `SelectedItem`? – Pavel Anikhouski Feb 02 '20 at 14:14
  • "well after searching for hours i found, that DataRowView is a way to access the Data which ist stored in the chosen Row of a DataGrid" - can you please share the source? I have some experience with WPF and didn't know that. The reference will be useful for other readers which need help – ASh Feb 02 '20 at 16:15
  • i believe it was [link](https://parallelcodes.com/wpf-datagrid-with-button/) – IRezzet Feb 02 '20 at 16:24

1 Answers1

0

Sooo I dont think anyone will care but here is my Solution to my own Problem:

The Xaml part stays the same. the Code behind:

private void Button_OnClick(object sender, RoutedEventArgs e)
        {
            Module module = (Module) ((Button) e.Source).DataContext;

            if (ChosenMods.Contains(module))
            {
                try
                {
                    MessageBox.Show("The Module `" + module.Name + "` is already chosen!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
            else
            {
                try
                {
                    ChosenMods.Add(module);
                    MessageBox.Show("The Module `" + module.Name + "` has been added!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }

I basically get the Datacontext of the Button with the Module module = (Module) ((Button) e.Source).DataContext;

I later to put it inside a List of Modules, which i reuse later.

The first problem was the initialization of my List which looked like this:

public List<Module> ChosenMods { get; set; }

but had to look like this:

public List<Module> ChosenMods = new List<Module>();

public List<Module> ChosenMods1
        {
            get => ChosenMods;
            set => ChosenMods = value;
        }

The second problem was, that i used DataRowView which, i realized, was absolutely pointless there.

Thanks for your answers anyway, have a nice Week.

Greetings, IRezzet

IRezzet
  • 13
  • 9