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