How do I add table header dynamically in c#? - c#

I have successfully created generating new records into table rows but want to add table header, so how to do that?
private void GenerateTable(int rowsCount)
{
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
const int colsCount = 3;
for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < colsCount; j++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
tb.ID = "TextBoxRow_" + i + "Col_" + j;
cell.Controls.Add(tb);
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
SetPreviousData(rowsCount, colsCount);
rowsCount++;
ViewState["RowsCount"] = rowsCount;
}

You do it like that:
TableHeaderRow header = new TableHeaderRow(); // Creating a header row
table.Rows.Add(header); // Add the header row to table tbl
For adding cells in the header row here is an example:
TableHeaderCell headerTableCell1 = new TableHeaderCell();
header.Cells.Add(headerTableCell1);
And this is where is goes in your code:
private void GenerateTable(int rowsCount)
{
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
const int colsCount = 3;
TableHeaderRow header = new TableHeaderRow();
table.Rows.Add(header);
//These two lines in iteir own loop
TableHeaderCell headerTableCell1 = new TableHeaderCell();
header.Cells.Add(headerTableCell1);
for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < colsCount; j++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
tb.ID = "TextBoxRow_" + i + "Col_" + j;
cell.Controls.Add(tb);
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
SetPreviousData(rowsCount, colsCount);
rowsCount++;
ViewState["RowsCount"] = rowsCount;
}

Building on the suggested solution by #israel-altar, I struggled with adding the header into a <thead> tag and not into a <tbody>.
To achieve this, you have to use the TableSelection property of your TableHeaderRow:
private void GenerateTable(List<string> fieldsList)
{
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
TableHeaderRow header = new TableHeaderRow();
foreach (string field in fieldsList)
{
TableHeaderCell headerTableCell1 = new TableHeaderCell();
headerTableCell1.Controls.Add(new Literal() {
Text = field
});
header.Cells.Add(headerTableCell1);
}
// Set TableSection to TableHeader so that the TableHeaderRow is added within <thead> and not within <tbody>
header.TableSection = TableRowSection.TableHeader;
table.Rows.Add(header);
// loop rows ...
}

Related

way to put linkbutton into tablecell, asp.net

Here is the part of a code which builds DB from SQL query, but I need to put extra cell for LinkButton.
for (int j = 0; j < numrows; j++)
{
TableRow row = new TableRow();
for (int i = 0; i < numcols; i++)
{
TableCell cell = new TableCell();
string textContent;
textContent = subjects.Rows[j][i].ToString();
cell.Text = textContent;
row.Cells.Add(cell);
}
TableCell cell1 = new TableCell();
LinkButton button = new LinkButton();
button.Text = "look";
button.Click += new EventHandler(submit_Click2);
cell1. = button;
row.Cells.Add(cell1);
table.Rows.Add(row);
}
The question is that I don't know what do to with this part of a code where I put button into cell, I couldn't find a way to do it. Is it possible to do it without GridView, because I can't use it?
TableCell cell1 = new TableCell();
LinkButton button = new LinkButton();
button.Text = "look";
button.Click += new EventHandler(submit_Click2);
cell1. = button;
Here you go, a simple example. But remember to not add the table to the page within IsPostBack
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//do not add dynamic controls like the linkbutton in the table in "IsPostBack"
}
//to generate some random data
var random = new Random();
//number of rows and cells
var rowCount = random.Next(5, 25);
var cellCount = random.Next(3, 10);
//create a new table
var table = new Table()
{
CssClass = "table table-striped"
};
//add the rows
for (int i = 0; i < rowCount; i++)
{
var row = new TableRow();
//add the cells
for (int j = 0; j < cellCount; j++)
{
//create a new cell
var cell = new TableCell();
//if the cell is not the last add content, otherwise add the linkbutton
if (j < cellCount - 1)
{
cell.Text = "CellValue " + random.Next(1000, 100000).ToString();
}
else
{
//create a new linkbutton
var lb = new LinkButton()
{
Text = "LinkButton"
};
//add the click event handler
lb.Click += new EventHandler(LinkButton_Click);
//add the linkbutton to the cell
cell.Controls.Add(lb);
}
//add the cell to the row
row.Cells.Add(cell);
}
//add the row to the table
table.Rows.Add(row);
}
//add the table to the page
PlaceHolder1.Controls.Add(table);
}

How to change the border color of a single cell using dynamic table

I am creating a dynamic table and I want to change the border color of a specific cell e.g (0,1).I cannot figure out on how will access the specific cell? Any idea?
protected void cmdCreate_Click(object sender, EventArgs e)
{
//clear the table
tbl.Controls.Clear();
int rows = Int32.Parse(txtRows.Text);
int cols = Int32.Parse(txtCols.Text);
for (int row = 0; row < rows; row++)
{
TableRow rowNew = new TableRow();
tbl.Controls.Add(rowNew);
tbl.Rows.AddAt(0, TableRow row);
for (int col = 0; col < cols; col++)
{
//create a new tablecell object
TableCell cellNew = new TableCell();
cellNew.Text = "Example Cell (" + row.ToString() + "," + col.ToString() + ")";
cellNew.BorderColor = System.Drawing.Color.Red;
// cellNew.Text += col.ToString() + ")";
if (chkBorder.Checked)
{
cellNew.BorderStyle = BorderStyle.Inset;
cellNew.BorderWidth = Unit.Pixel(1);
}
rowNew.Controls.Add(cellNew);
}
}
}
You can check which at which cell you are by checking row and col. And you need to specify the BorderStyle also, not just the color.
if (row == 0 && col == 1)
{
cellNew.BorderColor = System.Drawing.Color.Red;
cellNew.BorderStyle = BorderStyle.Solid;
}

how to get cell selected from dynamically generated html table with links

This is how im creating an table an adding link button for every cell. Im stuck in getting the selected cell value from the table
int noofquestions = 60;
int k = 1;
for (int i = 1; i <= noofquestions / 5; i++)
{
HtmlTableRow tRow = new HtmlTableRow();
//TableRow tRow = new TableRow();
tblNavigation.Rows.Add(tRow);
for (int j = 1; j <= 5; j++)
{
HtmlTableCell tCell = new HtmlTableCell();
//TableCell tCell = new TableCell();
//tCell.Text = "Row " + i + ", Cell " + j;
tRow.Cells.Add(tCell);
//tCell.InnerText = k.ToString();
LinkButton link = new LinkButton();
//HyperLink link = new HyperLink();
link.Text = k.ToString();
tCell.Controls.Add(link);
k = k + 1;
}

How to create nested html table in c# using recursion logic?

I just tried over it but i got the HTML tables in the manner of unique,i just need the tables to created one within another.
sample output: http://i.stack.imgur.com/drdXx.jpg][1]sample
In Design Page
<table id="tableContent" border="1" runat="server"></table>
protected void Page_Load(object sender, EventArgs e)
{
var s = 0;
Addtable(s);
}
public void Addtable(int j)
{
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
cell.InnerText = "col 1";
row.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = "col2";
row.Cells.Add(cell);
tableContent.Rows.Add(row);
tableContent.Border =1;
if( j < 5)
{
j++;
Addtable(j);
}
}
You need to change your recursion function as below
public void Addtable(HtmlTable baseTable, int j)
{
HtmlTable innerTable = new HtmlTable();
// change to stylesheet instead. Just added as an example to get the output
baseTable.Style.Add("margin-left", "25px");
baseTable.Style.Add("margin-right", "25px");
baseTable.Style.Add("margin-bottom", "25px");
baseTable.Style.Add("text-align", "center");
baseTable.Border = 1;
//Create a container cell for inner table
HtmlTableRow container = new HtmlTableRow();
HtmlTableCell containerCell = new HtmlTableCell();
Literal l = new Literal();
l.Text = "Table " + j;
containerCell.Controls.Add(l);
containerCell.Controls.Add(innerTable);
containerCell.ColSpan = 2;
container.Cells.Add(containerCell);
baseTable.Rows.Add(container);
if (j < 5)
{
j++;
Addtable(innerTable, j);
}
}
Then call the function
var s = 0;
Addtable(tableContent,s);

GridView cell width is always 0

I have a GridView, in which the width of each column is dynamic and changes depending on the DataSet. I need to get the width of each column, but the column/cells width is always '0', presumably because I haven't set the width property.
Is there a way around this?
here is the code:
gridResult.DataSource = dtResult;
gridResult.DataBind();
for (int i = 0; i < gridResult.Rows.Count; i++)
{
for (int j = 0; j < gridResult.Rows[i].Cells.Count; j++)
{
string encoded = gridResult.Rows[i].Cells[j].Text;
gridResult.Rows[i].Cells[j].Text = Context.Server.HtmlDecode(encoded);
}
}
GridView2.ShowHeaderWhenEmpty = true;
int tblCols = gridResult.HeaderRow.Cells.Count;
Table tbl = new Table();
TableRow tr = new TableRow();
double check = gridResult.Columns[0].ItemStyle.Width.Value;
double check2 = gridResult.Rows[0].Cells[0].Width.Value;
// TableRow tr = new TableRow();
for (int j = 0; j < tblCols; j++)
{
// Add the table to the placeholder control
TableCell td = new TableCell();
td.Text = gridResult.HeaderRow.Cells[j].Text;
td.Attributes.Add("width", gridResult.HeaderRow.Cells[j].Width + "px");
td.HorizontalAlign = HorizontalAlign.Center;
tr.Cells.Add(td);
td.BorderWidth = 1;
td.BorderColor = System.Drawing.Color.Black;
}
tbl.Rows.Add(tr);
PlaceHolder1.Controls.Add(tbl);

Categories