Jamie Kelly
Jamie Kelly

Reputation: 309

Programmatically deselect a row in a DataGrid

I'm struggling to find a solution to this.

What I am trying to do is to only make certain rows selectable in a DataGrid. The SelectionMode is FullRow. An example would be if a user tried to drag-select a few rows, and one of them I wouldn't want to be selectable. In this instance I would want the valid rows to be selected still, but not the invalid one.

Any ideas?

Upvotes: 2

Views: 3650

Answers (4)

user1760979
user1760979

Reputation:

This will work,

        int row = grdexam.SelectedIndex;
        DataGridRow rv =(DataGridRow)this.grdexam.ItemContainerGenerator.ContainerFromIndex(row);
        DataRowView rvv =(DataRowView)rv.Item;
        MessageBox.Show(rvv.Row[1].ToString());

Upvotes: 0

Arthur Nunes
Arthur Nunes

Reputation: 7058

This guy wanted to do something alike with a ListBox. I believe the solution can be adapted to work with the DataGrid as well.

EDIT

public static class DataGridRowEx
{
    public static bool GetCanSelect(DependencyObject obj)
    {
        return (bool)obj.GetValue(CanSelectProperty);
    }
    public static void SetCanSelect(DependencyObject obj, bool value)
    {
        obj.SetValue(CanSelectProperty, value);
    }
    public static readonly DependencyProperty CanSelectProperty =
        DependencyProperty.RegisterAttached("CanSelect", typeof(bool), typeof(DataGridRowEx), new UIPropertyMetadata(true, OnCanSelectChanged));

    private static void OnCanSelectChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        var item = sender as DataGridRow;
        if (item == null)
            return;

        if ((bool)args.NewValue)
        {
            item.Selected -= RowSelected;
        }
        else
        {
            item.Selected += RowSelected;
            item.IsSelected = false;
        }
    }

    private static void RowSelected(object sender, RoutedEventArgs e)
    {
        var item = sender as DataGridRow;
        if (item == null)
            return;

        item.Dispatcher.BeginInvoke((Action)(()=>
        item.IsSelected = false));
    }
}

To test it:

public class ViewModel : INotifyPropertyChanged
{
    #region INotifyPropertyChanged values

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion


    public List<Dummy> Elements { get; set; }

    public ViewModel()
    {
        this.Elements = new List<Dummy>(){
            new Dummy() { CanSelect =true, MyProperty = "Element1"},
            new Dummy() { CanSelect =false, MyProperty = "Element2"},
            new Dummy() { CanSelect =true, MyProperty = "Element3"},
            new Dummy() { CanSelect =false, MyProperty = "Element4"},
            new Dummy() { CanSelect =true, MyProperty = "Element5"},
            new Dummy() { CanSelect =true, MyProperty = "Element6"},
            new Dummy() { CanSelect =true, MyProperty = "Element7"},
            new Dummy() { CanSelect =true, MyProperty = "Element8"},
            new Dummy() { CanSelect =false, MyProperty = "Element9"},
        };
    }
}

public class Dummy
{
    public bool CanSelect { get; set; }

    public string MyProperty { get; set; }

    public override string ToString()
    {
        return this.MyProperty;
    }
}

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="525"
    Height="350"
    mc:Ignorable="d">
<Window.Resources>
    <Style x:Key="DataGridRowStyle" TargetType="{x:Type DataGridRow}">
        <Setter Property="local:DataGridRowEx.CanSelect" Value="{Binding CanSelect}" />
    </Style>
</Window.Resources>
<Window.DataContext>
    <local:ViewModel />
</Window.DataContext>
<Grid x:Name="LayoutRoot">
    <DataGrid ItemsSource="{Binding Elements}"
              RowStyle="{DynamicResource DataGridRowStyle}"
              SelectionUnit="FullRow" />
</Grid>
</Window>

It works with multiselection, even when pressing shift. The only significant difference from the solution for the ListBoxItem was that the unselection had to be queued using Dispatcher.BeginInvoke, not sure why. The only caveat with this approach is that trying to select an unselectable item unselects the current selected one if the DataGrid has single selection, or unselects all, if the DataGrid has extended selection.

Upvotes: 1

CR41G14
CR41G14

Reputation: 5594

Not a great solution but you could deselect the rows on a Mouse_Up event, so let the user select all then deselect programmatically, I have not tested this

private void dgvReport_MouseUp(object sender, MouseEventArgs e)
        {

            foreach (DataGridViewRow row in this.dgvReport.SelectedRows) {


                if (row.Cells[1].Value == "Invalid"){

                    this.dgvReport.Rows[row.Index].Selected = false;

                }


            }
        }

Upvotes: 0

Sayse
Sayse

Reputation: 43330

Not the best way to do this but you could make an inherted class that holds the selected index of the grid and then if an invalid row is selected you just change the selected index to the last valid index

Upvotes: 1

Related Questions