Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
hi every body
i have a page that i need to show searched data in it my database is excel suppose i don't know how many columns and rows i need to show all data in a table depend on number of columns if there is one column in excel sheet it drew a table with on columns um if they are column i excel sheet it draw 10 columns in page and so on the header of the table should be the same in excel
how can it be done in asp.net c# ?
Posted
Updated 30-Mar-16 8:51am
v2
Comments
Karthik_Mahalingam 30-Mar-16 12:59pm    
refer .net - How to read data from excel file using c# [^]to get data from excel

use GridView to display the entire data, depends on the columns in the excel the gridview will be generated dynamically.
MR.alaa 30-Mar-16 13:56pm    
i use something like that but i need to know how to appened data to html
foreach (DataColumn column in ds.Tables[0].Columns)
{

lblresult.Text = column.ColumnName;
}

to be like that
html.Append("<td>" + ds.Tables[0].Rows[i]["ID"] + "</td>");

this gets me header what about data in rows

foreach (DataRow row in ds.Tables[0].Rows)
{

lblresult.Text = ??? to get values;
}

Karthik_Mahalingam 30-Mar-16 14:28pm    
hi
pls use the reply button, so that the user will get notified. else it will be ignored.
Karthik_Mahalingam 30-Mar-16 14:32pm    
y cant you use gridview instead of Table, since utlimatly the GridView will render as Html Table only.
MR.alaa 30-Mar-16 14:30pm    
finally i write this code which works fine how to append results to html table to be shows or drown to user in asp page ??

1 solution

Try this sample code to create an HTML table from Datatable, modify it as per your need.

C#
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Columns.Add("Address");
dt.Rows.Add(1, "karthik", "bangalore");
dt.Rows.Add(2, "sehwag", "delhi");
dt.Rows.Add(3, "kavya", "coimbatore");

string html = "<table><thead  ><tr>";
foreach (DataColumn column in dt.Columns)
    html += "<td>" + column.ColumnName + "</td>";
html += "</tr></thead><tbody>";
foreach (DataRow row in dt.Rows)
{
    html += "<tr>";
    foreach (DataColumn col in dt.Columns)
        html += "<td>" + row[col] + "</td>";
    html += "</tr>";
}
html += "</tbody></table>";


lbl.Text = html;
 
Share this answer
 
Comments
Karthik_Mahalingam 30-Mar-16 14:56pm    
use this for border and header highlight
string html = @"<table border='1' cellpadding='0' cellspacing='1'><thead ><tr style='color:white;background-color:grey' >";

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900