Setting Mouse event to a specific GridTextColumn

Hi,

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?

Thanks,

Dov.


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 )
                {
                    break;
                }
            }
            return child;
        }





3 Replies

GT Gnanasownthari Thirugnanam Syncfusion Team 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. 

C# 
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; 
    else 
        ((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. 
Regards, 
Gnanasownthari T. 
 



DO Dov June 5, 2017 02:48 PM UTC

Thanks.
It's working great.

Dov.


SV Srinivasan Vasu Syncfusion Team June 6, 2017 05:11 AM UTC

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


Loader.
Up arrow icon