1

I'm unable to get value from selected cell

DataGrid

    <DataGrid x:Name="dataGrid" Margin="0,15,0,-0.333" GridLinesVisibility="None" HeadersVisibility="None" HorizontalAlignment="Right" RenderTransformOrigin="0.5,0.5" Width="225" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" Background="#FF6E6E6E" ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ColumnWidth="225" BorderBrush="{x:Null}" CellStyle="{StaticResource NameCellStyle}" AutoGenerateColumns="False" BorderThickness="0" SelectionChanged="dataGrid_SelectionChanged">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="location" Binding="{x:Null}" ClipboardContentBinding="{x:Null}" CanUserResize="False" Width="0" MinWidth="0"/>
                <DataGridTextColumn x:Name="title" Binding="{x:Null}" ClipboardContentBinding="{x:Null}" IsReadOnly="True" Width="225" CellStyle="{StaticResource MyDataGridCell}" FontSize="18"/>

            </DataGrid.Columns>
    </DataGrid>

I've tried following to get string from selected row

        int i = dataGrid.SelectedIndex;
        DataRowView v = (DataRowView)dataGrid.Items[i];
        string s = (string)v[0];
        MessageBox.Show(s);

Error

     "An unhandled exception of type 'System.InvalidCastException' occurred in MissionControl M.exe"
    "Additional information: Unable to cast object of type 'Item' to type 'System.Data.DataRowView'."

Some of the questions/solutions that doesn't work for me

WPF Datagrid Get Selected Cell Value https://social.msdn.microsoft.com/Forums/vstudio/en-US/d8434785-82b8-4e7a-8723-466de6b35845/how-to-get-cell-value-to-a-string-for-wpf-datagrid?forum=wpf

edit: I just moved to WPF from WinForms and I use the DataGrid as a menu. I'm trying to move my winfor project functionality to this WPF project.

Community
  • 1
  • 1
HPaulson
  • 83
  • 10
  • And what exactly do you mean by "not working"? Are you getting any exceptions/errors, or just not the value you're expecting to get? – Grx70 Dec 15 '14 at 09:50
  • Well the exception says it's not a string. Could you check what is actually in `v[0]`? – Moti Azu Dec 15 '14 at 09:58
  • @MotiAzu Well, all columns are of type DataGridTextColum – HPaulson Dec 15 '14 at 10:01
  • @HPaulson You are not looking at columns, you are looks at cells. Just check the value of `v[0]` without converting it to a string. – Moti Azu Dec 15 '14 at 10:05
  • WPF is different from winforms. Delete all your code and start reading [here](http://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx). – Federico Berasategui Dec 15 '14 at 10:07
  • @MotiAzu Taking from the message the exception occurs while casting `dataGrid.Items[i]` to `DataRowView` and not `v[0]` to `string`. That's because `dataGrid.Items` collection contains items of type `Item` (which I assume is OP's class). – Grx70 Dec 15 '14 at 10:22
  • @Grx70 Correct, my mistake. OP, updates on that? Are you trying to figure out what you put in there? – Moti Azu Dec 15 '14 at 10:23

0 Answers0