Datagrid row delete button with new row placeholder disable

Aim:

To create a button on a datagrid to delete a row but not crash on the new row placeholder row.

Method:

Add a column like any other inside the datagrid but use a template type to show the button.

                        
                            
                                
                            
                        
                    

Add a converter to check for the new row place holder:

    [ValueConversion(typeof(string), typeof(Boolean))]
    public class PlaceHolderConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value.ToString() == "{DataGrid.NewItemPlaceholder}")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception();
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *