We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Setting Mouse event to a specific GridTextColumn

Thread ID:





130803 Jun 4,2017 12:57 PM UTC Jun 6,2017 05:11 AM UTC WPF 3
Tags: SfDataGrid
Asked On June 4, 2017 12:57 PM UTC


I have a SfDataGrid with 3 GridTextColumns.
1 of them can't be edited, the other 2 can.

Now I see that 1 of the 2 editiable GridTextColumns can only be set to 1 or 0.

I have an old code of DataGrid that can switch between 1 and 0 by a button press on the cell.
I would like to implement the same thing in SfDataGrid in order to ease the data input to the user, so he won't need to actually type 1 or 0 just press on the cell and the switch will occur, the other editiable GridTextColumn can be any positive number.

I couldn't manipulate the old code to work on SfDataGrid.

Maybe you have a sample code that works?



My DataGrid code:

private void TableList_MouseLeftButtonDown( object sender, MouseButtonEventArgs e )
    foreach( DataGridCellInfo cellInfo in TableList.SelectedCells )
        // this changes the cell's content not the data item behind it
        DataGridCell gridCell = TryToFindGridCell( TableList, cellInfo );

        if( gridCell.Column.Header.ToString() == "Symbol" )
            if( ( gridCell.Content as TextBlock ).Text == "1" )
                ( gridCell.Content as TextBlock ).Text = "0";
            else if( ( gridCell.Content as TextBlock ).Text == "0" )
                ( gridCell.Content as TextBlock ).Text = "1";

            ( ( ListData )TableList.Items[TableList.Items.IndexOf( TableList.CurrentItem )] ).Symbol = ( gridCell.Content as TextBlock ).Text;

        static DataGridCell TryToFindGridCell( DataGrid grid, DataGridCellInfo cellInfo )
            DataGridCell result = null;
            DataGridRow row = ( DataGridRow )grid.ItemContainerGenerator.ContainerFromItem( cellInfo.Item );
            if( row != null )
                int columnIndex = grid.Columns.IndexOf( cellInfo.Column );
                if( columnIndex > -1 )
                    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>( row );
                    result = presenter.ItemContainerGenerator.ContainerFromIndex( columnIndex ) as DataGridCell;
            return result;

 static T GetVisualChild<T>( Visual parent ) where T : Visual
            T child = default( T );
            int numVisuals = VisualTreeHelper.GetChildrenCount( parent );
            for( int i = 0; i < numVisuals; i++ )
                Visual v = ( Visual )VisualTreeHelper.GetChild( parent, i );
                child = v as T;
                if( child == null )
                    child = GetVisualChild<T>( v );
                if( child != null )
            return child;

Gnanasownthari Thirugnanam [Syncfusion]
Replied On June 5, 2017 12:56 PM UTC

Hi Dov, 

Thank you for contacting Syncfusion support. 

We have analyzed your query, You can achieve your requirement “can switch between 1 and 0 by a button press on the cell in GridtextColumn” by CellTapped event as like below code example. 

this.datagrid.CellTapped += Datagrid_CellTapped;     
private void Datagrid_CellTapped(object sender, GridCellTappedEventArgs e) 
    var orderID = ((e.OriginalSender as GridCell).DataContext as OrderInfo).OrderID; 
    if(orderID == 0) 
        ((e.OriginalSender as GridCell).DataContext as OrderInfo).OrderID=1; 
        ((e.OriginalSender as GridCell).DataContext as OrderInfo).OrderID=0; 

We have prepared working sample for your  requirement, you can download the same from below mentioned location. 

Sample location: 
Note: Make sure in your side underline property is implemented by INotifyPropertyChanged event. 
Gnanasownthari T. 

Replied On June 5, 2017 02:48 PM UTC

It's working great.


Srinivasan Vasu [Syncfusion]
Replied On June 6, 2017 05:11 AM UTC

Hi Dov, 
Thanks for your update. 
Please let us know if you need further assitance on this. 


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

or the page will be automatically redirected to sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon