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:
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.