Forum guidelines
AuthorQuestion
Adding Table Rows Columns At Runtime in asp.net
Posted on: 05 Jan 2008

Hi All,

I want to add a table  to my form and also I need to enter data throught text box (s) into table whenever iI press add button using ASP.Net(Vb.net)  at dynamically. Every time I need to add 3-cells in a row whenever I hit Add button.  Please help me how to overcome this situation.

Please guide me.                                                                                                                                                    Thanks in Adv.   to everyone.                                                                                                                            kolags@hotmail.com

 


AuthorReply
Re: Adding Table Rows Columns At Runtime in asp.net
Posted on: 08 Jan 2008  
hiee,
i hope i got ur problem correctly..do lik this..
have a asp:Table in ur form and giv a id 2 it..

for example:<asp:Table id="tblName" Runat="server"></asp:Server>

in ur code behind file under button click event u can write lik this:
private void btn_Click(object sender, System.EventArgs e)
{
      TableRow tr = new  TableRow();
       tr.Cells.Add(new TableCell());
       tr.Cells.Add(new TableCell());
       tr.Cells.Add(new TableCell());
       tr.Cells[0].Text  = TextBox1.Text;
       tr.Cells[1].Text = TextBox2.Text;
       tr.Cells[2].Text = TextBox3.Text;
     //add the row to the existing table.
       this.tblName.Rows.Add(tr);
}
Hope this is wat u r lukin 4 and it works out..

Regards,
Soumya

Re: Adding Table Rows Columns At Runtime in asp.net
Posted on: 08 Jan 2008  

Hi,you were looking for the code in VB.Net,the code has given above is in C#,that is correct only it will work,but i am giving u code in VB.net also,but there is some changes,plz check it.do the same thing like ASP table n under the button event write this code.....

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim tr As New TableRow

tr.Cells.Add(New TableCell)

tr.Cells.Add(New TableCell())

tr.Cells.Add(New TableCell())

tr.Cells.Add(New TableCell())

Dim t As New TextBox

tr.Cells(0).Text = t.Text.Insert(0, "HI")

tr.Cells(1).Text = t.Text.Insert(0, "H0W")

tr.Cells(2).Text = t.Text.Insert(0, "ARE")

tr.Cells(3).Text = t.Text.Insert(0, "YOU")

Me.tab.Rows.Add(tr)

End Sub

Here am creating the TEXTBOX at the Run time,not in the Design time,this is the only change in the above code and as per ur reqirement its in VB.NET.

Regards

Arshad Khan


Re: Adding Table Rows Columns At Runtime in asp.net
Posted on: 08 Jan 2008  
thanks arshad.
Regards,
Niradhip(James Hetfield)
MCP,MVC,MVP(Mindcracker)
--------------------------------------------------------------------------------------
Please remember to click “Do you like this answer” on the post that helps you.
Re: Adding Table Rows Columns At Runtime in asp.net
Posted on: 22 Jan 2008  

Hi,

Thanks to everyone for providing valuable information. For time being, I have completed my task by datagrid with someone’s suggestion.

Always expecting guidance from superiors like you.

 

kolags@hotmail.com

Don't take any decisions when you are Angry,
Don't make any promises when you are Happy

 


Re: Adding Table Rows Columns At Runtime in asp.net
Posted on: 23 Jan 2008  
it's very easy to add rows in asp.net.By adding dtable as new table,drow as new row.....
Re: Adding Table Rows Columns At Runtime in asp.net
Posted on: 21 Aug 2008  
Thank you Sowmya, This code was helpfull for me.
thanks
Nataraj
Re: Adding Table Rows Columns At Runtime in asp.net
Posted on: 29 Jan 2009  
IN Design Mode  ADD the Code

<div align="left">
                                        <asp:Table ID="BooksTable" CellPadding="3" CellSpacing="0" runat="server" BorderWidth="1px"
                                            GridLines="Both">
                                        </asp:Table>
                                    </div>

Code :

using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

1.Get DataSet For the table From the database

DataTable objDataTable = new DataTable();
            DataSet objDataSet =new DataSet();
            objDataSet = .. retrive the table from database
            objDataTable = (DataTable)objDataSet.Tables[<<TableName>>].DefaultView.Table;
            TableRow objTableRow = new TableRow();
            TableCell objTableCell = new TableCell();
            TableHeaderCell objTableHeaderCell = new TableHeaderCell();
            if (objDataTable.Rows.Count <= 0)
            {
                //Code To Display Text When No Datas
                objTableRow = new TableRow();
                objTableHeaderCell = new TableHeaderCell();
                objTableHeaderCell.Text = "No Records";
                objTableHeaderCell.Style.Add("font-weight", "bold");
                objTableHeaderCell.Style.Add("text-align", "center");
                objTableRow.Cells.Add(objTableHeaderCell);
                BooksTable.Rows.Add(objTableRow);
                BooksTable.Width = Unit.Point(700);
            }
            else if (objDataTable.Rows.Count >= 1)
            {
                //Code to Add Heading In Table
                objTableRow = new TableRow();
                objTableHeaderCell = new TableHeaderCell();
                objTableHeaderCell.Text = "<h3><u><<Heading For Table>></u></h3>";
                objTableHeaderCell.Style.Add("font-weight", "bold");
                objTableHeaderCell.Style.Add("text-align", "center");
                objTableHeaderCell.Wrap = false;
                objTableHeaderCell.ColumnSpan = objDataTable.Columns.Count;
                objTableRow.Cells.Add(objTableHeaderCell);
                BooksTable.Rows.Add(objTableRow);
                //Code To Add ColumnNames In Table
                objTableRow = new TableRow();
                for (int iColumn = 0; iColumn < objDataTable.Columns.Count; iColumn++)
                {
                    objTableHeaderCell = new TableHeaderCell();
                    objTableHeaderCell.Text = objDataTable.Columns[iColumn].ColumnName.ToString();
                    objTableHeaderCell.Style.Add("font-weight", "bold");
                    objTableHeaderCell.Style.Add("text-align", "center");
                    objTableHeaderCell.Width = Unit.Pixel(120);
                    objTableHeaderCell.Wrap = false;
                    objTableRow.Cells.Add(objTableHeaderCell);
                }
                BooksTable.Width = Unit.Pixel(200 * objDataTable.Columns.Count);
                BooksTable.Rows.Add(objTableRow);
                //Code TO Add Datas in Table
                for (int iRow = 0; iRow < objDataTable.Rows.Count; iRow++)
                {
                    objTableRow = new TableRow();
                    for (int iColumn = 0; iColumn < objDataTable.Columns.Count; iColumn++)
                    {
                        objTableCell = new TableCell();
                        objTableCell.Text = objDataTable.Rows[iRow][iColumn].ToString();
                        objTableCell.Width = Unit.Pixel(120);
                        objTableCell.Wrap = false;
                        objTableRow.Cells.Add(objTableCell);
                    }
                    BooksTable.Rows.Add(objTableRow);
                }
            }
            //Code to Create HtmlPage for the Report
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            StringBuilder strData = new StringBuilder(string.Empty);
            StringBuilder strFunction = new StringBuilder(string.Empty);
            string strHTMLpath = HttpContext.Current.Server.MapPath("HtmlFile/PrintPage.htm");
            BooksTable.RenderControl(htw);
            StreamWriter strWriter = new StreamWriter(strHTMLpath, false, Encoding.Default);
            strData.Append("<html><body>");
            strData.Append(htw.InnerWriter.ToString());
            strData.Append("</body></html>");
            strWriter.Write(strData.ToString());
            strWriter.Close();
            strWriter.Dispose();

2. Code To Export The ASP Table to Excel or PDF

1. Download the iTextSharp.dll

if (ExportDropDownList.SelectedItem.Text == "Excel")
            {
                string sString = "";
                Response.ClearContent();
                Response.AddHeader("content-disposition", "attachment; filename=BooksReport.xls");
                Response.ContentType = "application/vnd.ms-excel";
                StringWriter sWriter = new StringWriter();
                HtmlTextWriter hWriter = new HtmlTextWriter(sWriter);
                BooksTable.RenderControl(hWriter);
                sString += sWriter.ToString();
                Response.Write(sString);
                Response.Flush();
            }
            else if (ExportDropDownList.SelectedItem.Text == "PDF")
            {
                Document document = new Document(PageSize.A4.Rotate());
                StringBuilder strData = new StringBuilder(string.Empty);
                string strHTMLpath = Server.MapPath("HtmlFile/ForPdfPage.htm");
                string strPDFpath = Server.MapPath("HtmlFile/BooksReport.pdf");
                StringWriter sw = new StringWriter();
                sw.WriteLine(Environment.NewLine);
                sw.WriteLine(Environment.NewLine);
                sw.WriteLine(Environment.NewLine);
                sw.WriteLine(Environment.NewLine);
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                BooksTable.RenderControl(htw);
                StreamWriter strWriter = new StreamWriter(strHTMLpath, false, Encoding.UTF8);
                strData.Append("<Center><b><u>Books Report</u></b></Center><br/><br/>");
                strData.Append(htw.InnerWriter.ToString());
                strData.Append("</body></html>");
                strWriter.Write(strData.ToString());
                strWriter.Close();
                strWriter.Dispose();
                iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
                PdfWriter.GetInstance(document, new FileStream(strPDFpath, FileMode.Create));
                document.Add(new Header(iTextSharp.text.html.Markup.HTML_ATTR_STYLESHEET, "Styles/PageCss.css"));
                document.Open();
                ArrayList objects;
                document.NewPage();
                objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.Default), styles);
                for (int k = 0; k < objects.Count; k++)
                    document.Add((IElement)objects[k]);
                document.Close();
                Response.Write(strPDFpath);
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AddHeader("Content-Disposition", "attachment; filename=BooksReport.pdf");
                Response.ContentType = "application/octet-stream";
                Response.WriteFile(strPDFpath);
                Response.Flush();
                Response.Close();
                if (File.Exists(strPDFpath))
                    File.Delete(strPDFpath);


prakash j

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.