Click here to Skip to main content
15,942,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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);
            }
      }
}


C#
public DataGridRow GetRow(int index)
{
      DataGridRow row = 
            (DataGridRow)DataGridControl.ItemContainerGenerator.ContainerFromIndex(index);
      if (row == null)
      {
            row = (DataGridRow)DataGridControl.ItemContainerGenerator.ContainerFromIndex(index);
      }
      return row;
}


C#
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
Updated 15-May-13 3:46am
v4
Comments
Pheonyx 15-May-13 9:33am    
What have you tried so far?
rohith naik 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<datagridcellspresenter>(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<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;
}

1 solution

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.
 
Share this answer
 
Comments
rohith naik 16-May-13 2:30am    
Thanks Pheonyx, I will try out the solution now. Will get back to you on this.
rohith naik 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 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900