Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to programatically disable a particular cell in WPF DataGrid

I m having a WPF DataGrid. Can u please tell , how to programatically disable a particular cell in WPF DataGrid.

like image 727
Abhi Avatar asked Oct 20 '11 12:10

Abhi


2 Answers

I'm answering this as I ran into the same issue, this is the solution I came up with.

You can't access cells and rows directly in WPF so we first define some helper extensions.

(Using some of the code from: http://techiethings.blogspot.com/2010/05/get-wpf-datagrid-row-and-cell.html)

public static class DataGridExtensions
{
    public 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;
    }

    public static DataGridRow GetRow(this DataGrid grid, int index)
    {
        DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {
            // May be virtualized, bring into view and try again.
            grid.UpdateLayout();
            grid.ScrollIntoView(grid.Items[index]);
            row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }

    public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
    {
        if (row != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

            if (presenter == null)
            {
                grid.ScrollIntoView(row, grid.Columns[column]);
                presenter = GetVisualChild<DataGridCellsPresenter>(row);
            }

            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            return cell;
        }
        return null;
    }

    public static DataGridCell GetCell(this DataGrid grid, int row, int column)
    {
        DataGridRow gridRow = GetRow(grid, row);
        return GetCell(grid, gridRow, column);
    }
}

With this we can get the cell at the first row, fifth column like this:

dataGrid1.GetCell(0, 4)

So to set the column to disabled is now really easy:

dataGrid1.GetCell(0, 4).IsEnabled = false;

Please note In some cases it is necessary for the form to load before any of this works.

Hope this helps someone someday ;-)

like image 149
Nebula Avatar answered Oct 07 '22 02:10

Nebula


using styles, as in the following:

<DataGrid.CellStyle>
    <Style TargetType="DataGridCell" >
        <Style.Setters>
            <Setter Property="IsEnabled" Value="False"/>
        </Style.Setters>
    </Style>
</DataGrid.CellStyle>
like image 29
MBDevelop Avatar answered Oct 07 '22 00:10

MBDevelop