Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
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:
XML
<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:

C#
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
Updated 12-Jun-11 21:19pm
v2

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:

C#
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
 
Share this answer
 
Comments
U@007 11-Jul-12 5:44am    
my side 5
:)
Add RowStyle to DataGrid
XML
<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;
           }
       }
 
Share this answer
 
Comments
Thomas Daniels 18-Dec-12 11:27am    
Why do you post an answer to a question from July? The problem is solved already.
Budokan1987 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900