<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: WPF DataGrid &#8211; detecting the column, cell and row that has been clicked</title>
	<atom:link href="http://www.scottlogic.co.uk/blog/colin/2008/12/wpf-datagrid-detecting-clicked-cell-and-row/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scottlogic.co.uk/blog/colin/2008/12/wpf-datagrid-detecting-clicked-cell-and-row/</link>
	<description>Colin Eberhardt&#039;s Adventures in .NET</description>
	<lastBuildDate>Thu, 09 Feb 2012 03:02:27 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Magesh_LB</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2008/12/wpf-datagrid-detecting-clicked-cell-and-row/#comment-95546</link>
		<dc:creator>Magesh_LB</dc:creator>
		<pubDate>Fri, 16 Dec 2011 09:42:30 +0000</pubDate>
		<guid isPermaLink="false">http://wpfadventures.wordpress.com/?p=59#comment-95546</guid>
		<description>Hi Collin,

By using the following simple code we can achieve it,

We can get column index and Row index
and We can get the Selected Cell value too.

To get Column Index: gridER.CurrentCell.Column.DisplayIndex
TO Get the Row Index : gridER.SelectedIndex

To Get Cell Value:

DataRowView drv = gridER.SelectedItem as DataRowView;
DataRow dr = drv.Row;
MessageBox.Show(drv.ItemArray[gridER.CurrentCell.Column.DisplayIndex].ToString());</description>
		<content:encoded><![CDATA[<p>Hi Collin,</p>
<p>By using the following simple code we can achieve it,</p>
<p>We can get column index and Row index<br />
and We can get the Selected Cell value too.</p>
<p>To get Column Index: gridER.CurrentCell.Column.DisplayIndex<br />
TO Get the Row Index : gridER.SelectedIndex</p>
<p>To Get Cell Value:</p>
<p>DataRowView drv = gridER.SelectedItem as DataRowView;<br />
DataRow dr = drv.Row;<br />
MessageBox.Show(drv.ItemArray[gridER.CurrentCell.Column.DisplayIndex].ToString());</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: abdou</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2008/12/wpf-datagrid-detecting-clicked-cell-and-row/#comment-95297</link>
		<dc:creator>abdou</dc:creator>
		<pubDate>Thu, 13 Oct 2011 16:04:13 +0000</pubDate>
		<guid isPermaLink="false">http://wpfadventures.wordpress.com/?p=59#comment-95297</guid>
		<description>Nice!</description>
		<content:encoded><![CDATA[<p>Nice!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bhushan</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2008/12/wpf-datagrid-detecting-clicked-cell-and-row/#comment-92414</link>
		<dc:creator>Bhushan</dc:creator>
		<pubDate>Mon, 26 Sep 2011 05:53:14 +0000</pubDate>
		<guid isPermaLink="false">http://wpfadventures.wordpress.com/?p=59#comment-92414</guid>
		<description>Nice article.Helped me to get row from mouse down e.original source</description>
		<content:encoded><![CDATA[<p>Nice article.Helped me to get row from mouse down e.original source</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Water Damage Restoration</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2008/12/wpf-datagrid-detecting-clicked-cell-and-row/#comment-60152</link>
		<dc:creator>Water Damage Restoration</dc:creator>
		<pubDate>Sun, 19 Jun 2011 16:20:27 +0000</pubDate>
		<guid isPermaLink="false">http://wpfadventures.wordpress.com/?p=59#comment-60152</guid>
		<description>Need a SL4 datagrid for each statement to loop thru my grid.</description>
		<content:encoded><![CDATA[<p>Need a SL4 datagrid for each statement to loop thru my grid.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2008/12/wpf-datagrid-detecting-clicked-cell-and-row/#comment-57256</link>
		<dc:creator>David</dc:creator>
		<pubDate>Wed, 01 Jun 2011 12:07:42 +0000</pubDate>
		<guid isPermaLink="false">http://wpfadventures.wordpress.com/?p=59#comment-57256</guid>
		<description>Thanks Colin for a great article. Here&#039;s a VB.net version for anyone who finds this article from your favorite search engine:

    Private Sub DataGrid_MouseRightButtonUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim dep As DependencyObject = DirectCast(e.OriginalSource, DependencyObject)

        &#039; iteratively traverse the visual tree
        While dep IsNot Nothing And Not TypeOf dep Is DataGridCell And Not TypeOf dep Is Primitives.DataGridColumnHeader
            dep = VisualTreeHelper.GetParent(dep)
        End While

        If dep IsNot Nothing Then
            If TypeOf dep Is Primitives.DataGridColumnHeader Then
                Dim columnHeader As Primitives.DataGridColumnHeader = DirectCast(dep, Primitives.DataGridColumnHeader)
                &#039; do something
            End If

            If TypeOf dep Is DataGridCell Then
                Dim cell As DataGridCell = DirectCast(dep, DataGridCell)
                &#039; do something
            End If
        End If
    End Sub

--

            If TypeOf dep Is DataGridCell Then
                Dim cell As DataGridCell = DirectCast(dep, DataGridCell)

                &#039; navigate further up the tree
                While dep IsNot Nothing And Not TypeOf dep Is DataGridRow
                    dep = VisualTreeHelper.GetParent(dep)
                End While

                Dim row As DataGridRow = DirectCast(dep, DataGridRow)
            End If

--

    Private Function FindRowIndex(ByVal row As DataGridRow) As Integer
        Dim dataGrid As DataGrid = DirectCast(ItemsControl.ItemsControlFromItemContainer(row), DataGrid)

        Dim index As Integer = dataGrid.ItemContainerGenerator.IndexFromContainer(row)

        Return index
    End Function

--

    Private Function ExtractBoundValue(ByVal row As DataGridRow, ByVal cell As DataGridCell) As Object
        &#039; find the column that this cell belongs to.
        Dim col As DataGridBoundColumn = DirectCast(cell.Column, DataGridBoundColumn)

        &#039; find the property that this column is bound to.
        Dim binding As Binding = DirectCast(col.Binding, Binding)
        Dim boundPropertyName As String = binding.Path.Path

        &#039; find the object that is related to this row.
        Dim data As Object = row.Item

        &#039; extract the property value
        Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(data)

        Dim prop As PropertyDescriptor = properties(boundPropertyName)
        Dim value As Object = prop.GetValue(data)

        Return value
    End Function</description>
		<content:encoded><![CDATA[<p>Thanks Colin for a great article. Here&#8217;s a VB.net version for anyone who finds this article from your favorite search engine:</p>
<p>    Private Sub DataGrid_MouseRightButtonUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)<br />
        Dim dep As DependencyObject = DirectCast(e.OriginalSource, DependencyObject)</p>
<p>        &#8216; iteratively traverse the visual tree<br />
        While dep IsNot Nothing And Not TypeOf dep Is DataGridCell And Not TypeOf dep Is Primitives.DataGridColumnHeader<br />
            dep = VisualTreeHelper.GetParent(dep)<br />
        End While</p>
<p>        If dep IsNot Nothing Then<br />
            If TypeOf dep Is Primitives.DataGridColumnHeader Then<br />
                Dim columnHeader As Primitives.DataGridColumnHeader = DirectCast(dep, Primitives.DataGridColumnHeader)<br />
                &#8216; do something<br />
            End If</p>
<p>            If TypeOf dep Is DataGridCell Then<br />
                Dim cell As DataGridCell = DirectCast(dep, DataGridCell)<br />
                &#8216; do something<br />
            End If<br />
        End If<br />
    End Sub</p>
<p>&#8211;</p>
<p>            If TypeOf dep Is DataGridCell Then<br />
                Dim cell As DataGridCell = DirectCast(dep, DataGridCell)</p>
<p>                &#8216; navigate further up the tree<br />
                While dep IsNot Nothing And Not TypeOf dep Is DataGridRow<br />
                    dep = VisualTreeHelper.GetParent(dep)<br />
                End While</p>
<p>                Dim row As DataGridRow = DirectCast(dep, DataGridRow)<br />
            End If</p>
<p>&#8211;</p>
<p>    Private Function FindRowIndex(ByVal row As DataGridRow) As Integer<br />
        Dim dataGrid As DataGrid = DirectCast(ItemsControl.ItemsControlFromItemContainer(row), DataGrid)</p>
<p>        Dim index As Integer = dataGrid.ItemContainerGenerator.IndexFromContainer(row)</p>
<p>        Return index<br />
    End Function</p>
<p>&#8211;</p>
<p>    Private Function ExtractBoundValue(ByVal row As DataGridRow, ByVal cell As DataGridCell) As Object<br />
        &#8216; find the column that this cell belongs to.<br />
        Dim col As DataGridBoundColumn = DirectCast(cell.Column, DataGridBoundColumn)</p>
<p>        &#8216; find the property that this column is bound to.<br />
        Dim binding As Binding = DirectCast(col.Binding, Binding)<br />
        Dim boundPropertyName As String = binding.Path.Path</p>
<p>        &#8216; find the object that is related to this row.<br />
        Dim data As Object = row.Item</p>
<p>        &#8216; extract the property value<br />
        Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(data)</p>
<p>        Dim prop As PropertyDescriptor = properties(boundPropertyName)<br />
        Dim value As Object = prop.GetValue(data)</p>
<p>        Return value<br />
    End Function</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ripu</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2008/12/wpf-datagrid-detecting-clicked-cell-and-row/#comment-43562</link>
		<dc:creator>Ripu</dc:creator>
		<pubDate>Sat, 26 Feb 2011 07:44:45 +0000</pubDate>
		<guid isPermaLink="false">http://wpfadventures.wordpress.com/?p=59#comment-43562</guid>
		<description>private int FindRowIndex(DataGridRow row)
{
    DataGrid dataGrid =
        ItemsControl.ItemsControlFromItemContainer(row)
        as DataGrid;
 
    int index = dataGrid.ItemContainerGenerator.
        IndexFromContainer(row);
 
    return index;
}
-------------hey this is giving me error that is:- Cannot convert type &#039;System.Windows.Controls.ItemsControl to &#039;System.Windows.Controls.DataGrid via a reference conversion,boxing conversion,unboxing conversion,wrapping conversion,or null type conversion-----------------
plz help me
thanks ------</description>
		<content:encoded><![CDATA[<p>private int FindRowIndex(DataGridRow row)<br />
{<br />
    DataGrid dataGrid =<br />
        ItemsControl.ItemsControlFromItemContainer(row)<br />
        as DataGrid;</p>
<p>    int index = dataGrid.ItemContainerGenerator.<br />
        IndexFromContainer(row);</p>
<p>    return index;<br />
}<br />
&#8212;&#8212;&#8212;&#8212;-hey this is giving me error that is:- Cannot convert type &#8216;System.Windows.Controls.ItemsControl to &#8216;System.Windows.Controls.DataGrid via a reference conversion,boxing conversion,unboxing conversion,wrapping conversion,or null type conversion&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
plz help me<br />
thanks &#8212;&#8212;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Colin Eberhardt</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2008/12/wpf-datagrid-detecting-clicked-cell-and-row/#comment-31269</link>
		<dc:creator>Colin Eberhardt</dc:creator>
		<pubDate>Mon, 20 Dec 2010 13:37:14 +0000</pubDate>
		<guid isPermaLink="false">http://wpfadventures.wordpress.com/?p=59#comment-31269</guid>
		<description>Sorry Raju,

I don&#039;t have time to look into this. Try the MSDN WPF Forums:

http://social.msdn.microsoft.com/forums/en/wpf/threads/

Colin E.</description>
		<content:encoded><![CDATA[<p>Sorry Raju,</p>
<p>I don&#8217;t have time to look into this. Try the MSDN WPF Forums:</p>
<p><a href="http://social.msdn.microsoft.com/forums/en/wpf/threads/" rel="nofollow">http://social.msdn.microsoft.com/forums/en/wpf/threads/</a></p>
<p>Colin E.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raju</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2008/12/wpf-datagrid-detecting-clicked-cell-and-row/#comment-31265</link>
		<dc:creator>Raju</dc:creator>
		<pubDate>Mon, 20 Dec 2010 12:24:41 +0000</pubDate>
		<guid isPermaLink="false">http://wpfadventures.wordpress.com/?p=59#comment-31265</guid>
		<description>Hi,

I created datagrid and if i select any one row i can get selected cell value. But i need to get all the cell value of that particular row. I tried with above code it will work for only selected cell. How to do for getting all the cell value of that particular row.
Thanks in advance.</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I created datagrid and if i select any one row i can get selected cell value. But i need to get all the cell value of that particular row. I tried with above code it will work for only selected cell. How to do for getting all the cell value of that particular row.<br />
Thanks in advance.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

