How to create a table based on a two-dimensional array? - javafx

private List<List<String>> tableOverallList;
i have a series of list in this list. it contains 8 values in each list. i need to place it in the table created. i would like to have 2 Rows with 8 columns. for the 1st Row i have this list.
String[] tableTitleList = {" Title", " (Re)set", " Obs", " Mean", " Std.Dev", " Min", " Max", " Unit"};
List<String> tabTitleList = Arrays.asList(tableTitleList);
help me to place the 1st list of values inside the List tableOverallList in the 2nd Row. i will try managing with the rest of the list.
PdfPTable table = new PdfPTable(3); // 3 columns.
PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 4"));
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 5"));
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 6"));
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 7"));
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 8"));
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.addCell(cell7);
table.addCell(cell8);
document.add(table);

That's really easy. So you have data in a nested list. For instance:
public List<List<String>> getData() {
List<List<String>> data = new ArrayList<List<String>>();
String[] tableTitleList = {" Title", " (Re)set", " Obs", " Mean", " Std.Dev", " Min", " Max", "Unit"};
data.add(Arrays.asList(tableTitleList));
for (int i = 0; i < 10; ) {
List<String> dataLine = new ArrayList<String>();
i++;
for (int j = 0; j < tableTitleList.length; j++) {
dataLine.add(tableTitleList[j] + " " + i);
}
data.add(dataLine);
}
return data;
}
This will return a set of data where the first record is a title row and the following 10 rows contain mock-up data. It is assumed that this is data you have.
Now when you want to render this data in a table, you do this:
PdfPTable table = new PdfPTable(8);
table.setWidthPercentage(100);
List<List<String>> dataset = getData();
for (List<String> record : dataset) {
for (String field : record) {
table.addCell(field);
}
}
document.add(table);
The result will look like this:
You can find the full source code here: ArrayToTable and this is the resulting PDF: array_to_table.pdf
If you only want 2 rows, a title row and a data row, change the following line:
for (int i = 0; i < 10; ) {
Into this:
for (int i = 0; i < 2; ) {
I've provided a more generic solution because it's more elegant to have generic code.

Related

ASP.NET Dynamically add rows into existing Table while looping through XML nodes

What im trying to do is to populate an empty Table with cells and the number of cells per row is 4 and the number of all cells is the number of nodes in btnNode
The empty table ID = "MainTable"
and so far i created this but it seems i get an error in
TableRowCollection rows = new TableRowCollection();
has no constructors defined.Searched in google and didnt find help to solve this issue
Here is my code :
private void linkBTN_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
string text = btn.Text.ToString();
XmlDocument clickDoc = new XmlDocument();
clickDoc.Load(Server.MapPath("~/ProductShow.xml"));
XmlNodeList btnNode = clickDoc.SelectNodes("products/" + text.ToString() + "/*");
int count = btnNode.Count;
TableRowCollection rows = new TableRowCollection();
if (count < 4)
{
TableRow row = new TableRow();
rows.Add(row);
}
else
{
for (int i = 0; i < count / 4; i++)
{
TableRow row = new TableRow();
rows.Add(row);
}
}
int j = 1;
foreach (XmlNode node in btnNode)
{
//TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.CssClass = "Cell";
LinkButton linkbtn = new LinkButton();
linkbtn.Text = node.InnerText;
linkbtn.Attributes.Add("runat", "server");
Image img = new Image();
cell.Controls.Add(linkbtn);
cell.Controls.Add(new LiteralControl("<br/>"));
cell.Controls.Add(img);
rows[j].Cells.Add(cell);
j++;
}
foreach (TableRow row in rows)
{
MainTable.Rows.Add(row);
}
}

Copying one datatable selected column to another data table

I have a datatable, then I have all the column names of the datatable as checkbox, I want to display only those column records, for which user selected from the checkbox:
Below is the code, but I am not getting the desired result
var values = "";
string clmnm = "";
for (int i = 0; i < interestedIN.Items.Count; i++)
{
if (interestedIN.Items[i].Selected)
{
values += interestedIN.Items[i].Value + ",";
}
}
values = values.TrimEnd(',');
string[] words = values.Split(',');
DataTable dt = new DataTable();
dt = (DataTable)Session["dataset"];
DataTable dt1 = new DataTable();
foreach (string word in words)
{
dt1.Columns.Add(word, typeof(string));
if (clmnm == string.Empty)
{
clmnm = word.Trim();
}
else
{
clmnm += "," +word.Trim();
}
}
foreach (DataRow dr in dt.Rows)
{
string[] split = clmnm.Split(',');
int j =0;
string str = "";
string str2 = "";
while( j < split.Length)
{
str = split[j].ToString();
if (str2 == string.Empty)
{
str2 = "dr[\""+str.ToString()+"\"]";
}
else
{
str2 += "," + "dr[\""+str.ToString()+"\"]";
}
j+=1;
}
dt1.Rows.Add(str2);
}
then I am trying to export the result as an excel sheet: but getting the below excel sheet:
There is a lot of changes to be done to your code. Lets begin with selected columns from CheckBoxList. Try using List or Arrays to store which columns that user wants. Like
List<string> columns = new List<string>();
Then store the selected columns into the columns list. Then you need to add columns to your new DataTable dt1. As,
DataTable dt1 = new DataTable();
for (int i = 0; i < interestedIN.Items.Count; i++)
{
if (interestedIN.Items[i].Selected) //If user selected this columns checkbox.
{
columns.Add(interestedIN.Items[i].Text.Trim()); //Storing values to List.
dt1.Columns.Add(interestedIN.Items[i].Text.Trim()); //Adding columns to the DataTable.
}
}
Then you can loop through your original DataTable dt and store the values as below.
foreach (DataRow dr in dt.Rows)
{
DataRow dr1 = dt1.NewRow(); // Create new row which should have identical structure for inserting.
foreach(string col in columns)
{
dr1(col) = dr(col);
}
dt1.Rows.Add(dr1); //Add the row with the contents to the table.
}

how to place two tables horizontally within a pdf file?

I have created two tables on pdf file. Now I wants to arrange them in horizontal position. That is table 1 should be at left of page, and table 2 should be at the exact right position of table 1. But when Iam doing, table 1 comes correctly, but table2 is not exact horizontal with table1.table 2 is just placed as left aligned. How can I place this table 2 as horizontally parallel with table1?
var doc1 = new Document(PageSize.A4);
PdfWriter.GetInstance(doc1, new FileStream(path + "/" + pdf_name + "", FileMode.Create));
doc1.Open();
var table1 = new PdfPTable(1); //table1
table1.HorizontalAlignment = Element.ALIGN_LEFT;
table1.SpacingBefore = 50;
table1.DefaultCell.Border = 1;
table1.WidthPercentage = 40;
PdfPCell cell = new PdfPCell(new Phrase(student_name, boldTableFont));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table1.AddCell(cell);
doc1.Add(table1);
var table2= new PdfPTable(1); //table2
table2.DefaultCell.Border = 1;
table2.HorizontalAlignment = 2;
table2.SpacingBefore = 50;
table2.WidthPercentage = 40;
PdfPCell cell21 = new PdfPCell(new Phrase("success", body));
cell21.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table2.AddCell(cell21);
doc1.Add(table2);
doc1.Close();
Create a new table with a single row and two columns.
Place first table in the first row first column and second table in the first row second column
Give the alignment of the first column as left , and for the other right
hope this helps..

How do I group rows in asp.net's datalist control?

For example in my database there are 96 rows and what I want to happen is to group rows by four(the resulting table will have 3 columns with 8 rows). ex
You might have a much easier time if you can get away with a control other than a DataList. For instance, you could use a Table server control like this:
<asp:Table ID="tblGrouped" runat="server"></asp:Table>
And then in code:
protected void LoadData() {
var items = MyDataSource.GetMyItems();
TableRow tr = null;
TableCell tc = null;
for (int i = 0; i < items.Count; i++) {
if (i % 12 == 0) {
tr = new TableRow();
tc = new TableCell();
tc.Text = items[i].MyProperty;
tr.Cells.Add(tc);
tblGrouped.Rows.Add(tr);
} else if (i % 4 == 0) {
tc = new TableCell();
tc.Text = items[i].MyProperty;
tr.Cells.Add(tc);
} else {
tc.Text += "<br />" + items[i].MyProperty;
}
}
}

Regarding binding array to gridview

Hi
I have created session array :
int[] a = (int[])Session["values"];
Now i have to bind this value to my gridview. I have one column (boundfield in my gridview) in gridview.
I used code to bind array to gridview as follows, but its giving last value only as i want all the values to bind :
for (int i = 0; i < a.Length; i++)
{
str = "select * from Quest_Info where Quest_id='" + a[i] + "' order by Quest_id";
SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
ds2 = new DataSet();
da2.Fill(ds2, "Result");
reviewgrid.DataSource = ds2;
reviewgrid.DataBind();
}
Which code will work for this?
Asp.net, c#
Thank You.
Your code may not work because you are trying to bind gridview in the for loop which keep repeating and will result to bind only one last array id in the end.
You may try to prepare query out front before binding the gridview as example below.
string questIds = string.Empty;
for (int i = 0; i < a.Length; i++)
{
if (questIds.Length > 0)
questIds += ", ";
questIds += a[i];
}
string strSQL = "select * from Quest_Info where Quest_id IN ("+ questIds + ") order by Quest_id";
SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
ds2 = new DataSet();
da2.Fill(ds2, "Result");
reviewgrid.DataSource = ds2;
reviewgrid.DataBind();

Resources