How can I partially change the border of a merged cell? - c#

So given this code:
var mergeCells = worksheet.Cells["A1:B5"];
mergeCells.Merge = true;
mergeCells.Style.Border.BorderAround(ExcelBorderStyle.Medium);
var notWorkingCell = worksheet.Cells["C1"];
notWorkingCell.Style.Border.Right.Style = ExcelBorderStyle.Medium;
notWorkingCell.Style.Border.Left.Style = ExcelBorderStyle.None; // <--This does not happen
I would expect that the left border arround notWorkingCell (C1) would be removed. This is not happening:
How can I partially change the border of a merged cell? With pure Excel it is possible.

Looks like the border styles need to be explicitly set on the cell address within the merged cell itself, not the ExcelRange:
var mergeCells = worksheet.Cells["A1:B5"];
mergeCells.Merge = true;
mergeCells.Style.Border.BorderAround(ExcelBorderStyle.Medium);
var notWorkingCell = worksheet.Cells["C1"];
notWorkingCell.Style.Border.Right.Style = ExcelBorderStyle.Medium;
var cellToLeft = notWorkingCell.Start;
worksheet.Cells[cellToLeft.Row, cellToLeft.Column - 1]
.Style.Border.Right.Style = ExcelBorderStyle.None;

Related

How to Assign Individual Colors to Bars in radhtmlchart?

I am trying to add different colors for each individual bar in radhtmlchart. I tried using below code but it is not working as expected. Any way to achieve this ?
Color[] barColors = new Color[8]{
Color.Purple,
Color.SteelBlue,
Color.Aqua,
Color.Yellow,
Color.Navy,
Color.Green,
Color.Blue,
Color.Red };
for (int i = 0; i < 8; i++)
{
BarSeries bs = new BarSeries();
bs.Appearance.FillStyle.BackgroundColor = barColors[i];
RadHtmlChartCurrentChart.PlotArea.Series.Add(bs);
}
RadHtmlChartCurrentChart.DataSource = datatable;
RadHtmlChartCurrentChart.DataBind();
I want graph to look like this.
I found a way to do this.
First add ColorField property to in telerik:ColumnSeries
<telerik:ColumnSeries DataFieldY="ModelPercentage" ColorField="AddColor">
Then define color values in Hex values using string array, code behind.
string[] barColors = new string[8]
{
"#ffb128","#ff8828", "#FFFF00", "#808000","#800000","#F333FF","#707AF3","#68E572"
};
Then add these color values in your data source as a column.
Here I am using data table so I added another column as color values.
CurrentvsPropose.Columns.Add("AddColor", typeof(string));
Adding data to data table before binding.
tableRowCvsP["AddColor"] = barColors[i];
I got the idea from here

How to properly traverse a DataGrid in C#?

I want to be able to set the background of a DataGrid's row.
My first thought was to do this:
//MapDisplay is a DataGrid
SolidColorBrush myBrush = new SolidColorBrush(Colors.Red);
mapDisplay.RowBackground = myBrush;
Now, this works but it will set the background for EVERY row in the DataGrid.
My next thought was to do this:
SolidColorBrush myBrush = new SolidColorBrush(Colors.Red);
foreach (DataGridRow x in mapDisplay.Items)
{
x.Background = myBrush;
}
However this doesn't cause any of the Rows' backgrounds to change, so I assume I'm doing something fundamentally wrong. How do I properly traverse a DataGrid's rows to set the background?
Your question is tagged WPF, this is the winforms answer...
DataGridView rows are styled with row templates (DataGridViewCellStyle class).
Below are a stitched together snippet group of code to add rows to a grid. theGrid is the control to which we are adding rows. Event is a POCO returned from the db.
var rowCellStyle = new DataGridViewCellStyle(theMessagesGrid.DefaultCellStyle)
{
BackColor = string.IsNullOrEmpty(conditions)
? theGrid.DefaultCellStyle.BackColor
: theColor,
SelectionForeColor = Color.WhiteSmoke,
SelectionBackColor = theGrid.DefaultCellStyle.SelectionBackColor,
};
var theRow = new DataGridViewRow
{
Height = theGrid.RowTemplate.Height,
DefaultCellStyle = rowCellStyle,
Tag = Event.GroupName
};
theRow.CreateCells(theGrid);
var cellData = new object[theRow.Cells.Count];
// fill out cell data
cellData[0] = ...;
cellData[1] = ...
theRow.SetValues(cellData);
// add row to grid
try
{
theGrid.Rows.Add(theRow);
if (currentMsg == Event.Pkey) theGrid.Rows[theGrid.Rows.Count - 1].Selected = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, #"Error Building Grid", MessageBoxButtons.OK, MessageBoxIcon.Warning);
throw;
}
WPF ought to have some sort of styling to apply to rows. Add form properties storing your row templates and then based upon conditions update the rowCellStyle.
To change background of a particular row, requires getting that row based on some value of its DataContext, or its index.
Then apply Trigger/DataTrigger to your RowStyle.
Programmatically, you can get DataGridRow based on Item using :
DataGridRow row = (DataGridRow) mapDisplay.ItemContainerGenerator.ContainerFromItem(item);
mapDisplay.Items will give you list of bounded items which can be Map objects, or in general Employee objects.
, and ContainerFromIndex() method based on index.
And now, correction in your code,
foreach (object o in mapDisplay.Items)
{
Map m = o as Map;
if (m == null) break;
if (m.AreaCode == 1234)
{
DataGridRow row = (DataGridRow)mapDisplay.ItemContainerGenerator.ContainerFromItem(m);
row.Background = Brushes.Yellow;
}
}

custom header names with SoftArtisans OfficeWriter?

I am using SoftArtisans OfficeWriter tool to create an excel file.
By Using DataImportProperties.UseColumnNames=true the column headers are populated with the class property names but I want to give custom column headers.
Any suggestions?
Donot use DataImportProperties.UseColumnNames=true.Instead use cell specific formatting since it is header so for every cell row/column number is known.ex.
Style headerStyle = wb.CreateStyle();
headerStyle.Font.Size = 10;
headerStyle.Font.Bold = true;
ws.Cells[1, 0].Value = "Name";
ws.Cells[1, 0].ApplyStyle(headerStyle);
You can also merge and group columns as :
ws[0, 0].Value = "Information";
Palette pal = wb.Palette;
Color group1Color = pal.GetClosestColor(255, 244, 205);
headerStyle.BackgroundColor = group1Color;
headerStyle.Font.Bold = true;
ws[0, 0].ApplyStyle(headerStyle);
ws.CreateArea(0, 0, 1, 13).MergeCells();
ws.GroupColumns(0, 12, true);
//True/false keeps the group collapsed/uncollpasedwhen user opens the workbook.

Add ToolStripMenuItem in new row when one row is filled inside ToolStrip

I have one form and inside form i have one toolstrip control, and i am adding ToolStripMenuItem dynamically.
What i want is when one is filled up, items should list in next row.
I tried this for increasing the height and width of form but adding items in new row not happening.
ToolStripItemCollection t_col = toolStripTaskBar.Items;
int _howMany = t_col.Count;
Rectangle mi_bounds = t_col[_howMany - 1].Bounds;
if (this.Width < (mi_bounds.X + mi_bounds.Width))
{
int minimumFormHeight = 80;
this.MinimumSize = new Size((mi_bounds.X + mi_bounds.Width), minimumFormHeight);
}
Let me know if you not understand what i want.
Any suggestion how can i achieve this.
Thank You.
You can use LayoutStyle property of ToolStrip. You need to set up it to Table and modify layout settings (specify rows and columns count).
You can do it like this:
this.toolStrip1.LayoutStyle = ToolStripLayoutStyle.Table;
var layoutSettings = (this.toolStrip1.LayoutSettings as TableLayoutSettings);
layoutSettings.ColumnCount = 3;
layoutSettings.RowCount = 3;
And the you can add new items to toolstrip:
var item = new ToolStripMenuItem(string.Format("item{0}", this.toolStrip1.Items.Count + 1));
this.toolStrip1.Items.Add(item);

how to bind DataTable to legend in mschart

I use excel to paint a pic as follow:
please pay attention to the area hightlight in red
my question is:
how to bind the datatable to legend in mschart?
so , except the legend color, also people can see the detail data from the legend.
or, you guys can tell is it feasible to bind in mschart?
Thanks in advance!
As far as I know there is nothing in the API that allows you to simply "bind" your DataTable to a legend.
But you should still be able to manage something like that with some code:
var l = chart1.Legends[0];
l.LegendStyle = LegendStyle.Table;
l.TableStyle = LegendTableStyle.Tall;
l.BorderColor = Color.OrangeRed;
l.Docking = Docking.Bottom;
l.LegendStyle = LegendStyle.Table;
l.HeaderSeparator = LegendSeparatorStyle.DashLine;
l.HeaderSeparatorColor = Color.Red;
var firstColumn = new LegendCellColumn();
l.ColumnType = LegendCellColumnType.SeriesSymbol;
l.CellColumns.Add(firstColumn);
var secondColumn = new LegendCellColumn();
l.ColumnType = LegendCellColumnType.Text;
secondColumn.Text = "#SER";
l.CellColumns.Add(secondColumn);
foreach (DataRow row in dt.Rows)
{
var column = new LegendCellColumn();
column.ColumnType = LegendCellColumnType.Text;
column.HeaderText = row["x"].ToString();
column.Text = "#VALY";
l.CellColumns.Add(column);
}
However, my suggestion would be to include the data in a separate control, rather than as part of the chart itself. It would be much easier to work with if you kept it in one of the .net controls that's meant for tabular data, whether you're in winforms or webforms.

Categories

Resources