Bibek Aryal Bibek Aryal - 3 months ago 21
ASP.NET (C#) Question

Remove the first column from datatable

I am using Datatable to get some values from a database table.

DataTable dt = this.GetData();
StringBuilder html = new StringBuilder();
html.Append("<table border = '1'>");
foreach (DataRow row in dt.Rows)
{
html.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
html.Append("<td>");
html.Append(row[column.ColumnName]);
html.Append("</td>");
}
html.Append("</tr>");
}
html.Append("</table>");


The above code gives me this output:
enter image description here
The column with 0 0 is generated from the ID column of my table. I didn't pass the ID through my Stored Procedure because I dont need to display them. Is there a way to remove the first column itself and prevent it from displaying?

Answer

you can also use FOR instead of FOREACH and start from 1 instead of 0

for(int i=1;i<dt.Columns.Count;i++){//do whatever}