Set Row height in Excel through OpenXML - c#

I have the code for generating an Excel sheet and I want to set the Row height there.
The code is as follows:
Columns lstColumns = sheet1.Worksheet.GetFirstChild<Columns>();
bool needToInsertColumns = false;
if (lstColumns == null)
{
lstColumns = new Columns();
needToInsertColumns = true;
}
lstColumns.Append(
new Column() { Min = 1, Max = 1, Width = 120, CustomWidth = true }
);
if (needToInsertColumns)
{
sheet1.Worksheet.InsertAt(lstColumns, 0);
}
// For rows.
Row row;
row = new Row() { RowIndex = 1 };
row.Height = 100;
sheetData.Append(row);
Through the following code I am able to set the width but not the height.

Do the following:
Row row;
row = new Row() { RowIndex = 1 };
row.Height = 10.0;
row.CustomHeight = true;
or, better:
var row = new Row { Height = 10.0, CustomHeight = true, RowIndex = 1 };
The CustomHeight property is important here.

Related

Remove white space between Chart Area and Legends

How to remove this large blank space between my chart area and the legends? See that I even tried different docking styles but the gap is still there.
Any ideas?
I import the data from a CSV file, generated by another application. Every column is a new data series but they are all added into the same chart and chartArea element.
// Load data from CSV into a DataTable
DataTable dataTable = ReadCsvFile(System.IO.Path.GetFileName("mqlWeekRace.txt"), false);
Chart chart1 = new Chart();
chart1.Height = 1000;
chart1.Width = 1500;
chart1.ChartAreas.Add("ChartArea1");
chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineWidth = 1;
chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dot;
chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineWidth = 1;
chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dot;
chart1.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = false;
// chart1.ChartAreas["ChartArea1"].AxisY.IsStartedFromZero = false;
// Line colors
string[] myColors = new string[] { "Blue", "DarkOrange", "Firebrick", "DarkSeaGreen", "DarkViolet", "Sienna",
"DarkOrchid", "IndianRed", "ForestGreen", "Teal", "MidnightBlue", "DimGray" };
int colorIndex = 0;
foreach (DataColumn col in dataTable.Columns)
{
if (!col.ColumnName.StartsWith("Data") && col.ColumnName.Length > 1)
{
chart1.Series.Add(col.ColumnName.ToString());
chart1.Series[col.ColumnName].ChartArea = "ChartArea1";
chart1.Legends.Add(new Legend(col.ColumnName));
chart1.Legends[col.ColumnName].LegendStyle = LegendStyle.Column;
chart1.Legends[col.ColumnName].Docking = Docking.Top;
chart1.Legends[col.ColumnName].Font = new Font(FontFamily.GenericSansSerif, 15);
chart1.Series[col.ColumnName].ChartType = SeriesChartType.Line;
chart1.Series[col.ColumnName].XValueMember = "Data";
chart1.Series[col.ColumnName].YValueMembers = col.ColumnName.ToString();
chart1.Series[col.ColumnName].XValueType = ChartValueType.Date;
chart1.Series[col.ColumnName].BorderWidth = 3;
chart1.Series[col.ColumnName].Color = System.Drawing.Color.FromName(myColors[colorIndex]);
colorIndex++;
}
}
chart1.DataManipulator.IsStartFromFirst = true;
chart1.DataSource = dataTable;
chart1.DataBind();

Crossout whole row

I try crossout row in table. Line must be over all cells in center vertical.
So I add new shape to cell, like this:
var cell = node as Cell;
var width = cell.CellFormat.Width;
var line = new Shape(args.Document, ShapeType.Line);
line.Width = width;
line.HorizontalAlignment = HorizontalAlignment.Center;
line.RelativeVerticalPosition = RelativeVerticalPosition.Paragraph;
line.Top = 5;
line.BehindText = true;
line.WrapType = WrapType.None;
line.StrokeColor = Color.Black;
line.Stroke.LineStyle = ShapeLineStyle.Single;
line.StrokeWeight = 1;
_builder.MoveTo(cell.LastParagraph);
_builder.InsertNode(line);
But this work only when I have single text line in cell, if is two or more line text, my crossout line is not center:
How fix it?
Maybe is other solution for crossout whole row?
You can build logic on the following code that inserts a full-width Line Shape at the middle of each Cell in Table.
Document doc = new Document("E:\\temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
foreach (Table table in doc.FirstSection.Body.Tables)
{
foreach (Row row in table.Rows)
{
foreach (Cell cell in row.Cells)
{
enumerator.Current = collector.GetEntity(cell.FirstParagraph);
while (enumerator.Type != LayoutEntityType.Cell)
{
enumerator.MoveParent();
}
double top = enumerator.Rectangle.Top + (enumerator.Rectangle.Height / 2);
double left = enumerator.Rectangle.Left;
double width = enumerator.Rectangle.Width;
builder.MoveTo(table.NextSibling);
Shape line = builder.InsertShape(ShapeType.Line, width, 0);
line.Top = top;
line.Left = left;
line.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
line.RelativeVerticalPosition = RelativeVerticalPosition.Page;
line.BehindText = true;
line.WrapType = WrapType.None;
line.StrokeColor = Color.Blue;
line.Stroke.LineStyle = ShapeLineStyle.Single;
line.StrokeWeight = 1;
}
}
}
doc.Save("E:\\temp\\19.1.docx");
Hope, this help. I work with Aspose as Developer Evangelist.

Minimum height of Row [Migradoc]

I want to set the minimum height of row. however it seems there is limit
i am using below code [http://forum.pdfsharp.de/viewtopic.php?f=2&t=2812 ]
var spacerRow = t1.AddRow();
spacerRow.Height = "0.1mm";
var para2 = new Paragraph();
para2.Format.LineSpacingRule = LineSpacingRule.Exactly;
para2.Format.LineSpacing = "0.1mm";
para2.Format.SpaceBefore = 0;
para2.Format.SpaceAfter = 0;
spacerRow.Cells[0].Add(para2);
but the height is not reducing any further.
the spacer row is between the borderd rows as show in attached picture.
If you want to do it for all rows:
Table table = new Table();
table.Format.Alignment = ParagraphAlignment.Center;
table.Rows.HeightRule = RowHeightRule.Exactly;
table.Rows.Height = 5;
For a single row:
row = table.AddRow();
row.HeightRule = RowHeightRule.Exactly;
row.Height = 5;

DataGridView not showing values in runtime created columns

I´m having a really wierd issue. I assign to my DataGridView a list of entities as a DataSource. I create some columns in runtime, and then, for each Row in the DataGridView, I complete the values of that new columns base on some values of some columns of the row.
The code works fine, because I´m displaying that same DataGridView in other forms. But in this new UserControl, it seems that it´s not showing any values on that new columns.
The wierd thing, is that the values are actually there, because when I do the foreach row loop, I have some acumulators int objects that shows the values in a textbox, and the values are correct.
I used a try and catch to see if something was wrong, but everything is fine.
I attached an image of what I´m getting.
Those two highlighted textboxes are the one that acummulates the values of those two columns..
As I said, the same code is working fine in other forms. Just in case, this UserControl is added to a panel in a form.
This is the code I use for the DataGridView:
public void Actualizar_grilla_prestamos()
{
dgv_Prestamos.DataSource = null;
dgv_Prestamos.Columns.Clear();
dgv_Prestamos.DataSource = lista_prestamos;
dgv_Prestamos.RowHeadersVisible = false;
//Agregar columna cuotas restantes
DataGridViewColumn cuotas_restantes = new DataGridViewColumn();
{
cuotas_restantes.HeaderText = "C. Rest.";
cuotas_restantes.Name = "cuotas_restantes";
cuotas_restantes.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
cuotas_restantes.CellTemplate = new DataGridViewTextBoxCell();
cuotas_restantes.ToolTipText = "Cantidad de cuotas restantes por cobrar";
}
dgv_Prestamos.Columns.Add(cuotas_restantes);
//Agregar columna tipo de tasa
DataGridViewColumn tipo_tasa = new DataGridViewColumn();
{
tipo_tasa.HeaderText = "Tipo tasa";
tipo_tasa.Name = "tipo_tasa";
tipo_tasa.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
tipo_tasa.CellTemplate = new DataGridViewTextBoxCell();
}
dgv_Prestamos.Columns.Add(tipo_tasa);
//Agregar columna garantes
DataGridViewColumn garantes = new DataGridViewColumn();
{
garantes.HeaderText = "Garantes";
garantes.Name = "garantes";
garantes.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
garantes.CellTemplate = new DataGridViewTextBoxCell();
}
dgv_Prestamos.Columns.Add(garantes);
dgv_Prestamos.Columns["garantes"].DisplayIndex = dgv_Prestamos.Columns["Cliente1"].Index;
//Agregar columna cuotas mora
DataGridViewColumn cuotas_mora = new DataGridViewColumn();
{
cuotas_mora.HeaderText = "C. Venc.";
cuotas_mora.Name = "cuotas_mora";
cuotas_mora.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
cuotas_mora.CellTemplate = new DataGridViewTextBoxCell();
cuotas_mora.ToolTipText = "Cantidad de cuotas vencidas";
}
dgv_Prestamos.Columns.Add(cuotas_mora);
int cant_total_cuotas_mora = 0;
int total_cuotas_restantes = 0;
foreach (DataGridViewRow r in dgv_Prestamos.Rows)
{
Estado_prestamo estado = (Estado_prestamo)dgv_Prestamos.Rows[r.Index].Cells["Estado_prestamo"].Value;
if (estado.id_estado_prestamo != 3)
{
var lista_cuotas = (System.Data.Objects.DataClasses.EntityCollection<Sistema_financiero.Cuota>)dgv_Prestamos.Rows[r.Index].Cells["Cuota"].Value;
dgv_Prestamos.Rows[r.Index].Cells["cuotas_mora"].Value = lista_cuotas.Where(x => x.pagada != true && x.fecha_vencimiento < DateTime.Now.Date).Count();
if (Convert.ToInt32(dgv_Prestamos.Rows[r.Index].Cells["cuotas_mora"].Value) > 0)
{
dgv_Prestamos.Rows[r.Index].Cells["cuotas_mora"].Style.ForeColor = Color.Red;
}
dgv_Prestamos.Rows[r.Index].Cells["cuotas_restantes"].Value = lista_cuotas.Where(x => x.pagada != true).Count();
}
else
{
dgv_Prestamos.Rows[r.Index].Cells["cuotas_mora"].Value = 0;
dgv_Prestamos.Rows[r.Index].Cells["cuotas_restantes"].Value = 0;
dgv_Prestamos.Rows[r.Index].Cells["cuotas_restantes"].Style.ForeColor = Color.Green;
}
if (Convert.ToBoolean(dgv_Prestamos.Rows[r.Index].Cells["tasa_fija"].Value) == true)
{
dgv_Prestamos.Rows[r.Index].Cells["tipo_tasa"].Value = "FIJA";
}
else
{
dgv_Prestamos.Rows[r.Index].Cells["tipo_tasa"].Value = "VARIABLE";
}
dgv_Prestamos.Rows[r.Index].Cells["garantes"].Value = ((System.Data.Objects.DataClasses.EntityCollection<Sistema_financiero.Cliente>)dgv_Prestamos.Rows[r.Index].Cells["Cliente1"].Value).Count;
cant_total_cuotas_mora = cant_total_cuotas_mora + Convert.ToInt32(dgv_Prestamos.Rows[r.Index].Cells["cuotas_mora"].Value);
total_cuotas_restantes = total_cuotas_restantes + Convert.ToInt32(dgv_Prestamos.Rows[r.Index].Cells["cuotas_restantes"].Value);
}
tbx_Cuotas_adeudadas_vencidas.Text = cant_total_cuotas_mora.ToString();
tbx_Total_cuotas_restantes.Text = total_cuotas_restantes.ToString();
//Agregar columna ver prestamo
DataGridViewImageColumn ver_prestamo = new DataGridViewImageColumn();
{
ver_prestamo.HeaderText = "";
ver_prestamo.Name = "ver_prestamo";
ver_prestamo.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
ver_prestamo.CellTemplate = new DataGridViewImageCell();
ver_prestamo.Image = Properties.Resources.eye_small_grid;
ver_prestamo.ToolTipText = "Ver préstamo";
}
dgv_Prestamos.Columns.Add(ver_prestamo);
dgv_Prestamos.Columns["ver_prestamo"].DisplayIndex = 0;
dgv_Prestamos.Columns["id_prestamo"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
dgv_Prestamos.Columns["num_cuotas"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
dgv_Prestamos.Columns["cuotas_mora"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
dgv_Prestamos.Columns["cuotas_restantes"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
dgv_Prestamos.Columns["importe"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
dgv_Prestamos.Columns["Moneda"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dgv_Prestamos.Columns["Sistema_amortizacion"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
dgv_Prestamos.Columns["cuotas_mora"].DisplayIndex = dgv_Prestamos.Columns["num_cuotas"].Index + 1;
dgv_Prestamos.Columns["cuotas_restantes"].DisplayIndex = dgv_Prestamos.Columns["num_cuotas"].Index + 1;
dgv_Prestamos.Columns["importe"].DisplayIndex = dgv_Prestamos.Columns["garantes"].DisplayIndex;
dgv_Prestamos.Columns["num_cuotas"].DisplayIndex = dgv_Prestamos.Columns["cuotas_restantes"].DisplayIndex;
dgv_Prestamos.Columns["Estado_prestamo"].DisplayIndex = dgv_Prestamos.Columns[dgv_Prestamos.Columns.Count - 1].Index;
dgv_Prestamos.Columns["Moneda"].DisplayIndex = dgv_Prestamos.Columns[dgv_Prestamos.Columns.Count - 2].Index;
dgv_Prestamos.Columns["tipo_tasa"].DisplayIndex = dgv_Prestamos.Columns["tasa_fija"].Index;
List<int> lista_columnas_visibles = new List<int> { dgv_Prestamos.Columns["Estado_prestamo"].Index, dgv_Prestamos.Columns["garantes"].Index, dgv_Prestamos.Columns["importe"].Index, dgv_Prestamos.Columns["Sistema_amortizacion"].Index, dgv_Prestamos.Columns["tipo_tasa"].Index, dgv_Prestamos.Columns["Moneda"].Index, dgv_Prestamos.Columns["id_prestamo"].Index, dgv_Prestamos.Columns["num_cuotas"].Index, dgv_Prestamos.Columns["cuotas_mora"].Index, dgv_Prestamos.Columns["cuotas_restantes"].Index, dgv_Prestamos.Columns["ver_prestamo"].Index };
Mostrar_ocultar_columnas(dgv_Prestamos, lista_columnas_visibles);
dgv_Prestamos.Columns["num_cuotas"].HeaderText = "Cuotas";
dgv_Prestamos.Columns["id_prestamo"].HeaderText = "Nº";
dgv_Prestamos.Columns["tasa_fija"].HeaderText = "Tipo tasa";
dgv_Prestamos.Columns["importe"].HeaderText = "Importe";
dgv_Prestamos.Columns["Estado_prestamo"].HeaderText = "Estado";
dgv_Prestamos.Columns["Sistema_amortizacion"].HeaderText = "Amortización";
dgv_Prestamos.Columns["importe"].DefaultCellStyle.Format = String.Format("$ ##0.##");
if (dgv_Prestamos.Columns["Moneda"].Width > 99)
{
dgv_Prestamos.Columns["Moneda"].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
dgv_Prestamos.Columns["Moneda"].Width = 99;
}
}
You can see that the DataGridView has a column with an eye. If you click the eye, you can see that entity in another form. If you change the entity state in that form, it returns a value (borrado) with true if changes has been made, or false if no change has been made.
If changes were detected, then I call the above method again. Magically, it shows all that missing values!
private void dgv_Prestamos_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1)
{
if (e.ColumnIndex == dgv_Prestamos.Columns["ver_prestamo"].Index)
{
frm_Ver_Prestamo ver_prestamo = new frm_Ver_Prestamo();
ver_prestamo.prestamo_seleccionado = (Prestamo)dgv_Prestamos.Rows[e.RowIndex].DataBoundItem;
ver_prestamo.db = db;
ver_prestamo.ShowDialog();
if (ver_prestamo.borrado == true)
{
dgv_Prestamos.DataSource = null;
Cursor.Current = Cursors.WaitCursor;
Actualizar_grilla_prestamos();
Cursor.Current = Cursors.Default;
}
}
}
}
ver_prestamo is the form that shows the entity. I don´t know what make that work, the only difference is that I do a DataSource = null before. But I do that in the method anyways..
Have you tried moving the dgv_Prestamos.DataSource = lista_prestamos; to the bottom of Actualizar_grilla_prestamos.
I think that it is doing the binding when you set the datasource and can't see the columns you have created afterward.
"Construct the grid, set the DataSource, then change the appearances in the DataBindingComplete event." - glace

Creating new column doesn't display correct number of item

I have a list (collection), a total of 7 item which I'm looping through to create a gridview inside a listview. I also have a check that for example if the row count is 5, this will create a new column by adding 1 to it. The problem is when the row count is equal to 5 and is true, the first column will have the 7 items regardless and when the second column is created, it doesn't show the remaining 2 items. Instead it shows all 7 items again.
Can anyone help, what am I doing wrong?
foreach (var item in lstOfSoda)
{
GridLength rowheight1 = new GridLength();
RowDefinition rowDef1 = new RowDefinition { Height = rowheight1 };
grdSoda.RowDefinitions.Add(rowDef1);
GridLength rowheight2 = new GridLength();
RowDefinition rowDef2 = new RowDefinition { Height = rowheight2 };
grdSoda.RowDefinitions.Add(rowDef2);
GridLength columnwidth1 = new GridLength(231);
ColumnDefinition colDef1 = new ColumnDefinition { Width = columnwidth1 };
grdSoda.ColumnDefinitions.Add(colDef1);
GridLength columnwidth2 = new GridLength(5);
ColumnDefinition colDef2 = new ColumnDefinition { Width = columnwidth2 };
grdSoda.ColumnDefinitions.Add(colDef2);
ListView lvSoda = new ListView();
lvSoda.SetValue(Grid.RowProperty, row + 1);
lvSoda.SetValue(Grid.ColumnProperty, column);
grdSoda.Children.Add(lvSoda);
GridView gridView = new GridView();
GridViewColumn gvcSodaName = new GridViewColumn();
gvcSodaName.DisplayMemberBinding = new Binding("SodaName");
gridView.Columns.Add(gvcSodaName);
GridViewColumn gvcSodaFlavor = new GridViewColumn();
gvcSodaFlavor.DisplayMemberBinding = new Binding("SodaFlavor");
gridView.Columns.Add(gvcSodaFlavor);
lvSoda.ItemsSource = lstOfSoda;
lvSoda.View = gridView;
rowCount++;
if (rowCount == 5)
{
column = column + 2;
rowCount = 0;
}
}

Categories