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.
Unfortunately, activation email could not send to your email. Please try again.

Setting Mouse event to a specific GridTextColumn

Thread ID:

Created:

Updated:

Platform:

Replies:

130803 Jun 4,2017 08:57 AM Jun 6,2017 01:11 AM WPF 3
loading
Tags: SfDataGrid
Dov
Asked On June 4, 2017 08:57 AM

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;
        }





Gnanasownthari Thirugnanam [Syncfusion]
Replied On June 5, 2017 08:56 AM

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. 
 


Dov
Replied On June 5, 2017 10:48 AM

Thanks.
It's working great.

Dov.

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

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


CONFIRMATION

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.

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.

;