Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF DataGrid
I want to select multiple cells of a datagrid from code behind. My input to the function is a List<Tuple<int,int>> which contains the positions of all the cells I want to highlight in row,column format.
 
Basically, I want to highlight it in such a way such that any previously selected cells are unselected and the cells in the input list are selected.
 
It would also be nice if the SelectedCellsChanged event is fired. I dont care if it fired multiple times also.
 
I am unable to find any examples of this
 
Please help.
 
-------------------- Added your code from your comment -------
 
basically i know how to change the background colour of the cell. I can get the cell using this method (already using this), you can ignore the highlight flag stuff as it is specific to my app.
 
public void ColourCell(int row, int column, Color color, bool HighlightFlag = true)
{
     DataGridRow rowContainer = GetRow(row);
 
     if (rowContainer != null)
     {
            DataGridCellsPresenter presenter = GetVisualChild(rowContainer);
        // try to get the cell but it may possibly be virtualized but enable virtualizion is off
            DataGridCell cell = 
                   (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            // now try to bring into view and retreive the cell
            if (HighlightFlag == false)
                    cell.Background = new SolidColorBrush(Colors.MediumPurple);
            else
            {
                    cell.Background = new SolidColorBrush(color);
            }
      }
}
 
public DataGridRow GetRow(int index)
{
      DataGridRow row = 
            (DataGridRow)DataGridControl.ItemContainerGenerator.ContainerFromIndex(index);
      if (row == null)
      {
            row = (DataGridRow)DataGridControl.ItemContainerGenerator.ContainerFromIndex(index);
      }
      return row;
}
 
static T GetVisualChild(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(v);
            }
            if (child != null)
            {
                    break;
            }
      }
      return child;
}
Posted 15-May-13 4:00am
Edited 15-May-13 4:46am
Pheonyx11.8K
v4
Comments
Pheonyx at 15-May-13 9:33am
   
What have you tried so far?
rohith naik at 15-May-13 9:40am
   
basically i know how to change the background colour of the cell. I can get the cell using this method (already using this), you can ignore the highlight flag stuff as it is specific to my app.
 
public void ColourCell(int row, int column, Color color, bool HighlightFlag = true)
{
DataGridRow rowContainer = GetRow(row);
 
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild(rowContainer);
// try to get the cell but it may possibly be virtualized but enable virtualizion is off
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
// now try to bring into view and retreive the cell
if (HighlightFlag == false)
cell.Background = new SolidColorBrush(Colors.MediumPurple);
else
{
cell.Background = new SolidColorBrush(color);
}
}
}
public DataGridRow GetRow(int index)
{
DataGridRow row = (DataGridRow)DataGridControl.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
row = (DataGridRow)DataGridControl.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}

 

static T GetVisualChild(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(v);
}
if (child != null)
{
break;
}
}
return child;
}

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Have a read of this if you have not already:
http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.selectedcells.aspx[^]
and
http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid(v=vs.100).aspx[^]
Make sure your selection unit on the datagrid is set to Cell.
 
I would look at the "IsSelected" property on the cell objects. If you clear all selected cells at the start then as you perform your highlighting on specific cells set the "IsSelected" option to true.
  Permalink  
Comments
rohith naik at 16-May-13 2:30am
   
Thanks Pheonyx, I will try out the solution now. Will get back to you on this.
rohith naik at 16-May-13 2:40am
   
Do i need to add the DataGridCellInfo for the specified cells into SelectedCells or will be automatically done if I set the IsSelected option to true ?
Pheonyx at 16-May-13 4:05am
   
I do not know, try it and see what happens.
I believe the "SelectedCells" is a read only collection so you might not be able to add to it manually. but it is not something I have tried.
rohith naik at 20-May-13 3:39am
   
Thanks, this works. No need to add to the cell to SelectedCells and it is read only as you said. Very helpful mate :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 15 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100