Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF
Hi,
 
I am need to get a selected row value on double click of datagrid row. I found we need mousedouble click for this. Mouse double click event fires wherever we click on grid. To get value only on selection of the datagrid row I have written the following code
 
XAML:
<datagrid autogeneratecolumns="False" name="dgTitlesPendingListofTitles" canuseraddrows="False" itemssource="{Binding}" mousedoubleclick="dgTitlesPendingListofTitles_MouseDoubleClick">
                        <datagrid.columns>
                            <datagridtextcolumn header="Artists-Titles" binding="{Binding Path=ArtistTitle}" visibility="Visible" width="*" isreadonly="False" />
                            </datagrid.columns>
                    </datagrid>
 
Code behind:
 
private void dgTitlesPendingListofTitles_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            IInputElement element = e.MouseDevice.DirectlyOver;
            if (element != null && element is FrameworkElement)
            {
                if (((FrameworkElement)element).Parent is DataGridCell)
                {
                    var grid = sender as DataGrid;
                    if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1)
                    {
                        var rowview = grid.SelectedItem as DataRowView;
                        if (rowview != null)
                        {
                            DataRow row = rowview.Row;
                        }
                    }
                }
            }
}
 
I am getting grid.SelectedItems.Count =0 so it is not going into that loop.Can anyone help me out with a solution or what is the mistake I am doing here.
Posted 12-Jun-11 22:14pm
Edited 12-Jun-11 22:19pm
Tarun.K.S42.1K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Getting the selected element from DirectlyOver will return an IInputElement where the Parent is always null, therefore your second if loop will never get entered. If you only want to get the selected row from the DoubleClickEvent then you can do this:
 
private void dgTitlesPendingListofTitles_MouseDoubleClick(object sender, MouseButtonEventArgs e)
       {
           if (sender != null)
           {
               DataGrid grid = sender as DataGrid;
               if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1)
               {
                   DataGridRow dgr = grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem) as DataGridRow;
               }
           }
       }
 
Hope this helps
  Permalink  
Comments
U@007 at 11-Jul-12 5:44am
   
my side 5
:)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Add RowStyle to DataGrid
<DataGrid AutoGenerateColumns="False" Grid.Row="2" Margin="8,22,12,36" Name="resultDataGrid" IsReadOnly="True" >
            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <EventSetter Event="MouseDoubleClick" Handler="resultDataGrid_MouseDoubleClick"/>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
And in code behind:
 private void resultDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (sender != null)
            {
                    DataGridRow dgr = sender as DataGridRow;
            }
        }
  Permalink  
Comments
ProgramFOX at 18-Dec-12 11:27am
   
Why do you post an answer to a question from July? The problem is solved already.
Budokan1987 at 14-Jan-13 15:50pm
   
How can i get some value of the row? For example, if i have the following row: FirstName|LastName|Passport|Email and i want to take only the Passport, how can i do it? Thanks.
ErrCode at 20-Jun-13 1:48am
   
You should be able to access your Passport field like so:
 
private void resultDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var row = sender as DataGridRow;
if (row != null)
{
var item = row.DataContext as Person; // replace Person with your object type
if (item != null)
{
var passport = item.Passport;
}
}
}
 
PS: Upvoted the RowStyle solution because it seems more elegant IMO.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 385
1 Sergey Alexandrovich Kryukov 329
2 CPallini 270
3 DamithSL 214
4 Maciej Los 192
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100