0

Here is what I did using WPF / C#:

  1. Use DataGrid to display data from a DataTable. This step works fine.
  2. Add a ContextMenu to the DataGrid. This also seems to be fine as the context menu show up correctly.
  3. Use the context menu to access the cell selected in the DataGrid, and use the value of the cell for further work. This is the step I got stuck on.

I have been researching and trying other similar SO questions in the past couple of days but no luck. Can someone please help me get this right?

I use Visual Studio 2019 and it is a WPF Framework project. I tried the approach from dsfgsho. See link Getting WPF Data Grid Context Menu Click Row

//  Originally from dsfgsho, with slight changes.

private void MenuItem_Click(object sender, RoutedEventArgs e)
{
    //Get the clicked MenuItem
    var menuItem = (MenuItem)sender;

    //Get the ContextMenu to which the menuItem belongs
    var contextMenu = (ContextMenu)menuItem.Parent;

    //Find the placementTarget
    var item = (DataGrid)contextMenu.PlacementTarget;

    //Get the underlying item, that you cast to your object that is bound
    //to the DataGrid (and has subject and state as property)
    var selected_tbl_name = (DataColumn)item.SelectedCells[0].Item;
    //****The last step is where I get confused and it also throws an exception. 
}

XAML:

<Grid>
    <DataGrid 
        x:Name="dataGrid_test" 
        ItemsSource="{Binding}" 
        SelectedItem="{Binding SelectedItemPorperty}"
        HorizontalAlignment="Left" 
        Height="369" 
        Margin="395,10,0,0" 
        VerticalAlignment="Top" 
        Width="381" 
        SelectionChanged="DataGrid_SelectionChanged"
        >
        <DataGrid.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Select" Click="MenuItem_Click">
                </MenuItem>
            </ContextMenu>
        </DataGrid.ContextMenu>
    </DataGrid>
</Grid>

Regarding the last line of the function MenuItem_Click(object sender, RoutedEventArgs e): Since I am using the DataGrid with a DataTable, I am thinking the underlying item would be a DataTable cell. Is there a type for DataTable cell? I can't find it so I used DataCloumn. But an exception was thrown and it says "Specified argument was out of ranges of valid values. Parameter name: index".

DavidY
  • 357
  • 5
  • 15
  • `item.SelectedCells[0].Item` is a row item, the same object as `SelectedItemPorperty`. That's the whole object, not just the property or field you clicked on. I'd put a breakpoint in the Click handler and examine everything you can. `item.SelectedCells[0].Column` will give you a DataGridColumn, so you can get the DisplayIndex, Header, etc. In the debugger, you can see a property called `item.SelectedCells[0].ItemInfo`, but the compiler insists it isn't there. You might be able to get there via reflection, but it's undocumented and may vanish in a future version. – 15ee8f99-57ff-4f92-890c-b56153 May 24 '19 at 20:02
  • "Cell value" is a slippery concept with the WPF DataGrid. There isn't really any such thing. You can get the `DataGridCell` (that's the type for a cell that you asked about) one way or another and if its Content is a TextBlock or TextBox, you can cast that and get the text. But it might be templated. How to get at what's in there will depend on the column type (DataGridTextColumn, DataGridTemplateColumn, etc.). Where you go with this will depend on how you're populating the DataGrid. There isn't anything simple and straightforward. – 15ee8f99-57ff-4f92-890c-b56153 May 24 '19 at 20:14
  • @EdPlunkett I am populating the `DataGrid` with a `DataTable`. I tend to associate `DataGrid` as another layer of data presentation for types such as `DataTable`. I obviously need to understand the concept better. But what is the type of the Content in this case? Alternatively, maybe I should use a different type for data source that is easier to bind? – DavidY May 25 '19 at 01:59

0 Answers0