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.
Unfortunately, activation email could not send to your email. Please try again.

Setting background gradient color and border for a range of cells

Thread ID:

Created:

Updated:

Platform:

Replies:

90560 Oct 7,2009 12:08 PM Oct 16,2009 05:36 AM WPF 8
loading
Tags: GridControl
Nazim YENIER
Asked On October 7, 2009 12:08 PM

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

Clay Burch [Syncfusion]
Replied On October 8, 2009 03:23 AM

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-!

Nazim YENIER
Replied On October 8, 2009 03:56 AM

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)

Clay Burch [Syncfusion]
Replied On October 8, 2009 10:36 AM

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();
}
}

Nazim YENIER
Replied On October 8, 2009 10:52 AM

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

Clay Burch [Syncfusion]
Replied On October 8, 2009 11:01 AM

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();
}
}

Nazim YENIER
Replied On October 8, 2009 11:12 AM

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

Nazim YENIER
Replied On October 8, 2009 11:23 AM

"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

Mohamed Suhaib Fahad A. [Syncfusion]
Replied On October 16, 2009 05:36 AM

Hi Nazim,

Thanks for your feedbacks.

Thanks,
Fahad

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;