We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Setting background gradient color and border for a range of cells

Hi,
I have menu items to specify
- gradient background colors
- border thickness and border colors

Now, I need to assign a gradient brush to a range of cells (and not to each of the cells separately).

Similarly I need to assign border color and thickness to a range of cells (and not to each of the cells separately)

Could you give me an example doing this when the user clicks something please ? You can use a button or context menu to assign the specified properties to the selected range.

Example:
1. Select a range of cells
2. Right-click and context menu opens
3. Select "Green-6" menu item

and the border of the selected range becomes green with a thickness of 6.

Thanks a lot

Nazim YENIER
SAGE

8 Replies

CB Clay Burch Syncfusion Team October 8, 2009 07:23 AM UTC

Take a look at the last sample toward the bottom of this forum thread. I think it does something close to what you described.
http://www.syncfusion.com/support/forums/grid-wpf/89471/problem-with-covered-cells-and-gradient-backgrounds-!


NY Nazim YENIER October 8, 2009 07:56 AM UTC

I know this example well, since I had asked the question.
This time I want to assign gradient backgrounds to a range of individual cells (not to covered cells).

I tried several things but I didn't find how to do that. What to change in tnat code to do the same for cell ranges.
(same question for range borders)


CB Clay Burch Syncfusion Team October 8, 2009 02:36 PM UTC

The GridRangeInfo class has methods, GetFirstCell and GetNextCell, that allow you to iterate through the cells in a range.

So, try code like this as the menu handler in that sample:

void menuClick(object sender, RoutedEventArgs e)
{
//deactivate currentcell so changes will apply to cell
grid.CurrentCell.Deactivate();
MenuItem item = e.Source as MenuItem;
if (item.Header.ToString() == "item1")
{
MessageBox.Show(string.Format("Item1 clicked {0}", mouseDownCell));
}
else if (item.Header.ToString() == "item2")
{
MessageBox.Show(string.Format("Item2 clicked {0}", mouseDownCell));
}
else if (item.Header.ToString() == "Red Covered Cells"
|| item.Header.ToString() == "Blue Covered Cells")
{

grid.Focus();
GridRangeInfo range = grid.Model.Selections.Ranges.ActiveRange; //get the range
range = range.ExpandRange(1, 1, grid.Model.RowCount, grid.Model.ColumnCount); //make it a cell range
Brush b = item.Header.ToString() == "Red Covered Cells" ?
new LinearGradientBrush(Colors.Red, Colors.Pink, 90) :
new LinearGradientBrush(Colors.Blue, Colors.AliceBlue, 90);
int row, col;
if (range.GetFirstCell(out row, out col))
{
grid.Model[row, col].Background = b;
grid.Model[row, col].Borders.Right = new Pen(Brushes.Black, 2);
grid.Model[row, col].Borders.Bottom = new Pen(Brushes.Black, 2);
while (range.GetNextCell(ref row, ref col))
{
grid.Model[row, col].Background = b;
grid.Model[row, col].Borders.Right = new Pen(Brushes.Black, 2);
grid.Model[row, col].Borders.Bottom = new Pen(Brushes.Black, 2);
}
}
grid.InvalidateVisual();
}
}


NY Nazim YENIER October 8, 2009 02:52 PM UTC

This doesn't work :((
The result is in the attachment.

Do I have to modify grid_PrepareRenderCell method also ?

Nazim YENIER
SAGE



Cells range gradient color result_aacfb5b3.zip


CB Clay Burch Syncfusion Team October 8, 2009 03:01 PM UTC

Try using CellSpanBackgrounds to see if they give you what you want.

void menuClick(object sender, RoutedEventArgs e)
{
//deactivate currentcell so changes will apply to cell
grid.CurrentCell.Deactivate();
MenuItem item = e.Source as MenuItem;
if (item.Header.ToString() == "item1")
{
MessageBox.Show(string.Format("Item1 clicked {0}", mouseDownCell));
}
else if (item.Header.ToString() == "item2")
{
MessageBox.Show(string.Format("Item2 clicked {0}", mouseDownCell));
}
else if (item.Header.ToString() == "Red Covered Cells"
|| item.Header.ToString() == "Blue Covered Cells")
{

grid.Focus();
GridRangeInfo range = grid.Model.Selections.Ranges.ActiveRange; //get the range
range = range.ExpandRange(1, 1, grid.Model.RowCount, grid.Model.ColumnCount); //make it a cell range
Brush b = item.Header.ToString() == "Red Covered Cells" ?
new LinearGradientBrush(Colors.Red, Colors.Pink, 90) :
new LinearGradientBrush(Colors.Blue, Colors.AliceBlue, 90);

CellSpanBackgroundInfo info = new CellSpanBackgroundInfo(range.Top, range.Left, range.Bottom, range.Right, false, false, b, new Pen(Brushes.Black, 2));
grid.Model.CellSpanBackgrounds.Add(info);

grid.InvalidateVisual();
}
}


NY Nazim YENIER October 8, 2009 03:12 PM UTC

Maybe I wasn't clear in my question.

The gradient color must be applied to the cell range as if the range was a covered cells region. But the cells stay "not covered", independant one from each other.

I attached an example image of what I desire as result.

Nazim YENIER
SAGE



Desired result_63401eba.zip


NY Nazim YENIER October 8, 2009 03:23 PM UTC

"Try using CellSpanBackgrounds to see if they give you what you want. "


JUST GREAT !
It works !
Thanks a lot Clay for these fast answers.

Nazim YENIER
SAGE


MS Mohamed Suhaib Fahad A. Syncfusion Team October 16, 2009 09:36 AM UTC

Hi Nazim,

Thanks for your feedbacks.

Thanks,
Fahad

Loader.
Live Chat Icon For mobile
Up arrow icon