for (var i = 0; i <= data.GetUpperBound(0); i++)
{
var tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
for (var j = 0; j <= data.GetUpperBound(1); j++)
{
var tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
tc.Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(data[i, j]))));
tr.Append(tc);
}
table.Append(tr);
}
I want to change fontsize in table cell. Can you help me with that? I don't know why they didn't add a property for cell fontsize.
To change the fontsize of a table cell, you need to add a RunProperties to the Run. The fontsize is specified inside a FontSize element inside that RunProperties.
For example to change all of your entries to fontsize 18, your code would look like:
for (var i = 0; i <= data.GetUpperBound(0); i++)
{
var tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
for (var j = 0; j <= data.GetUpperBound(1); j++)
{
var tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
var run = new DocumentFormat.OpenXml.Wordprocessing.Run();
var text = new DocumentFormat.OpenXml.Wordprocessing.Text(data[i, j]);
// your old code for reference: tc.Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(data[i, j]))));
RunProperties runProperties1 = new RunProperties();
FontSize fontSize1 = new FontSize(){ Val = "36" };
runProperties1.Append(fontSize1);
run.Append(runProperties1);
run.Append(text);
paragraph.Append(run);
tc.Append(paragraph);
tr.Append(tc);
}
table.Append(tr);
}
Related
I'm using Grid and trying to make control dynamically in loop.
There are 3 Grids, and after clicking on the Grid3 control, I would like to get the control values of Grid1 and Grid2.
Please see image.
Any help is appreciated. Thanks!
code behind:
Dictionary<string, string[]> myDictionary3 = _Model.getRoomStatusData(_Model.RoomTypesSel, _Model.StartTimeNames);
//Grid 1 x:Name="timelist"
for (int i = 0; i < _Model.StartTimeNames.Length; i++)
{
Label time = new Label() { Text = _Model.StartTimeNames[i], WidthRequest = 80 };
timelist.Children.Add(time, i, 0);
}
//Grid 2 x:Name="roomtype"
for (int i = 0; i < roomtypes.Length; i++)
{
roomtype.RowDefinitions.Add(new RowDefinition());
Label room = new Label() { Text = _Model.RoomTypesSel[i].Name, WidthRequest = 80 };
roomtype.Children.Add(room, 0, i);
}
List<string[]> roomstatu = new List<string[]>()
{
new string[] {"1","2","3","4","5","6"}, new string[] {"1","2","3","4","5","6"},new string[] {"1","2","3","4","5","6"},new string[] {"1","2","3","4","5","6"},new string[] {"1","2","3","4","5","6"},new string[] {"1","2","3","4","5","6"},new string[] {"1","2","3","4","5","6"},new string[] {"1","2","3","4","5","6"}
};
//Grid 3 x:Name="status"
int index = 0;
foreach (var item in roomstatu)
{
status.RowDefinitions.Add(new RowDefinition());
for (int i = 0; i < item.Length; i++)
{
status.ColumnDefinitions.Add(new ColumnDefinition());
Label statuslabel = new Label() { Text = item[i], WidthRequest = 80};
status.Children.Add(statuslabel, i, index);
}
index++;
}
At first, you can get the index of the label user tapped in the status grid. And then you can get the label's row position and column position by the index.
I have done a sample and it worked well, you can try the following code:
int index = 0;
foreach (var item in roomstatu)
{
status.RowDefinitions.Add(new RowDefinition());
for (int i = 0; i < item.Length; i++)
{
status.ColumnDefinitions.Add(new ColumnDefinition());
Label statuslabel = new Label() { Text = item[i], WidthRequest = 80 };
status.Children.Add(statuslabel, i, index);
statuslabel.GestureRecognizers.Add((new TapGestureRecognizer
{
Command = new Command(async (obj) =>
{
Label temp = obj as Label;
Grid gridtemp = temp.Parent as Grid;
int position = gridtemp.Children.IndexOf(temp);
int columnindex = position % timelist.Children.Count;
int rowindex = (position / roomtype.Children.Count) +1;
string timevalue = ((Label)timelist.Children[columnindex]).Text;
string roomtypealue = ((Label)roomtype.Children[rowindex]).Text;
temp.Text = timevalue + "+"+ roomtypealue;
}),
CommandParameter = statuslabel,
}));
}
index++;
}
I am coding a chart. This is my code:
for (int i = 0; i < 2; i++)
{
Val= new ChartValues<ObservablePoint>(); // added
Val.Clear(); // added
lineSeries = new LineSeries
{
Title = "Graph " + i,
Values = Val,
Stroke = Brushes.Blue,
Fill = Brushes.Transparent,
LineSmoothness = 0,
PointGeometry = null
};
for (int jj = 0; jj < 2; jj++)
{
Val.Add(new ObservablePoint
{
X = jj+i+1,
Y = jj+i+1
});
}
Col.Add(lineSeries);
}
The problem is that in my Col variable (collection) all series are getting the latest values of the loop, which results are lineSeries are the same.
When I use the lineSeries = new LineSeries{}, it should create a new independent object right? Or do I have to erase and create it?
Any help is appreciated.
The issue is that you're reusing the same variable Val to store the actual values. Where is it declared, is it a list? Create a new one each time inside the outer loop and then use it to construct the LineSeries.
for (int i = 0; i < 2; i++)
{
var Val = new ChartValues<ObservablePoint>();
for (int jj = 0; jj < 2; jj++)
{
Val.Add(new ObservablePoint
{
X = jj+i+1,
Y = jj+i+1
});
}
var lineSeries = new LineSeries
{
Title = "Graph " + i,
Values = Val,
Stroke = Brushes.Blue,
Fill = Brushes.Transparent,
LineSmoothness = 0,
PointGeometry = null
};
Col.Add(lineSeries);
}
the problem is in line series, you use the same variable, and you are adding the same variable. It it changing each time, and in the end have the last data
lineSeries = new LineSeries
{
......
};
should be
var lineSeries = new LineSeries
{
......
};
I have created a flow document code-behind. I use a table with four columns.
But the fourth column didn't appear when printing
It seems that the pagewidth is just over half page
When I reduce columnwidth of columns - they appear but the text is wrapping
Here is the code I have
PrintDialog printDlg = new PrintDialog();
FlowDocument FlowTabledoc = new FlowDocument();
// FlowTabledoc.PageWidth = Double.NaN; dit not work
FlowTabledoc.PageWidth = printDlg.PrintableAreaWidth;
Table logflowtable = new Table();
FlowTabledoc.Blocks.Add(logflowtable);
logflowtable.CellSpacing = 5;
int numberOfColumns = 4;
for (int x = 0; x < numberOfColumns; x++)
{
logflowtable.Columns.Add(new TableColumn());
}
logflowtable.Columns[0].Width = new GridLength(60.0, GridUnitType.Pixel);
logflowtable.Columns[1].Width = new GridLength(120.0, GridUnitType.Pixel);
logflowtable.Columns[2].Width = new GridLength(190.0, GridUnitType.Pixel);
logflowtable.Columns[3].Width = new GridLength(90.0, GridUnitType.Pixel);
logflowtable.RowGroups.Add(new TableRowGroup());
int RowAnzahl = LogTB.Rows.Count;
int ColAnzahl = LogTB.Columns.Count;
Paragraph Abschnitt = new Paragraph();
Abschnitt.FontSize = 14;
for (int r = 0; r <= RowAnzahl - 1; r++)
{
logflowtable.RowGroups[0].Rows.Add(new TableRow());
currentRow = logflowtable.RowGroups[0].Rows[r+1];
for (int c = 0; c <= ColAnzahl - 1; c++)
{
currentRow.FontSize = 14;
currentRow.FontWeight = FontWeights.Normal;
currentRow.Cells.Add(new TableCell(new Paragraph(new Run(LogTB.Rows[r][c].ToString()))));
}
FlowTabledoc.Blocks.Add(Abschnitt);
}
FlowTabledoc.Name = "FlowDoc";
IDocumentPaginatorSource idpSource = FlowTabledoc;
printDlg.PrintDocument(idpSource.DocumentPaginator, "Title-Header");
}
}
Solution found:
FlowTabledoc.ColumnWidth = 793;
I am trying to create a word table from xml file but the rows are not in their proper order,ie the first element occurs as last row, 2nd as 1st row and so on. Where am i going wrong?
Word.Table oTable;
Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oTable = oDoc.Tables.Add(wrdRng, ds.Tables[0].Rows.Count, ds.Tables[0].Columns.Count, ref oMissing, ref oMissing);
oTable.Range.ParagraphFormat.SpaceAfter = 6;
oTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
oTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
for (int r = 1; r <= ds.Tables[0].Rows.Count; r++)
{
for (int c = 1; c <= ds.Tables[0].Columns.Count; c++)
{
oTable.Cell(r, c).Range.Text = ds.Tables[0].Rows[r-1].ItemArray[c-1].ToString();
}
}
Here is the sample code to create simple word file with Table. It will give you a loop hint how to proceed with it.
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
Section s = doc.AddSection();
Table table = s.AddTable(true);
//Create Header and Data
String[] header = { "Item", "Description", "Qty", "Unit Price", "Price" };
String[][] data = {
new String[]{ "Spire.Doc for .NET",".NET Word Component","1","$799.00","$799.00"},
new String[]{"Spire.XLS for .NET",".NET Excel Component","2","$799.00","$1,598.00"},
new String[]{"Spire.Office for .NET",".NET Office Component","1","$1,899.00","$1,899.00"},
new String[]{"Spire.PDF for .NET",".NET PDFComponent","2","$599.00","$1,198.00"},
};
//Add Cells
table.ResetCells(data.Length + 1, header.Length);
//Header Row
TableRow fRow = table.Rows[0];
fRow.IsHeader = true;
//Row Height
fRow.Height = 23;
//Header Format
fRow.RowFormat.BackColor = Color.AliceBlue;
for (int i = 0; i < header.Length; i++)
{
//Cell Alignment
Paragraph p = fRow.Cells[i].AddParagraph();
fRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Data Format
TextRange TR = p.AppendText(header[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 14;
TR.CharacterFormat.TextColor = Color.Teal;
TR.CharacterFormat.Bold = true;
}
//Data Row
for (int r = 0; r < data.Length; r++)
{
TableRow dataRow = table.Rows[r + 1];
//Row Height
dataRow.Height = 20;
//C Represents Column.
for (int c = 0; c < data[r].Length; c++)
{
//Cell Alignment
dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
//Fill Data in Rows
Paragraph p2 = dataRow.Cells[c].AddParagraph();
TextRange TR2 = p2.AppendText(data[r][c]);
//Format Cells
p2.Format.HorizontalAlignment = HorizontalAlignment.Center;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 12;
TR2.CharacterFormat.TextColor = Color.Brown;
}
}
//Save and Launch
doc.SaveToFile("WordTable.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("WordTable.docx");
Console.ReadLine();
}
I want to dynamically create a excel-like table with datagridview, but I want to have the option to set colspan to some columns.
The idea is just to display data, the user will not type anything, but it should be in table/spreadsheet look. If I cannot have datagridview with colspan is there any other type of table-like tool which has a colspan?
I other hand the columns will be created dynamically from database query result.
I'm using windows forms.
Any ideas?
You might take a look at the TableLayoutPanel Class, which has a TableLayoutPanel.SetColumnSpan Method.
Here's a code sample, which spans the one of the text boxes on 2 columns:
var dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Value1");
dt.Columns.Add("Value2");
dt.Rows.Add(1, "aa", "xx");
dt.Rows.Add(2, "bb","yy");
dt.Rows.Add(3, "cc", "zz");
tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
tableLayoutPanel1.ColumnCount = 4;
tableLayoutPanel1.AutoSize = true;
for (int i = 0; i < dt.Columns.Count; i++)
{
var l = new Label();
l.Dock = DockStyle.Fill;
l.Text = dt.Columns[i].ColumnName;
tableLayoutPanel1.Controls.Add(l, i, 0);
}
var emptyLabel = new Label();
emptyLabel.Text = "Empty label";
tableLayoutPanel1.Controls.Add(emptyLabel, 4, 0);
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Rows[i].ItemArray.Length; j++)
{
var tb = new TextBox();
tb.Multiline = true;
tb.Dock = DockStyle.Fill;
tb.Text = dt.Rows[i][j].ToString();
tableLayoutPanel1.Controls.Add(tb, j, i+1);
if (i == 1 && j == 2)
tableLayoutPanel1.SetColumnSpan(tb, 2);
}
}