Reputation: 930
I have a stored procedure with which I use the data to populate a table with a Datatable as follows:
foreach (DataRow row in dt.Rows)
{
html.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
html.Append("<td>");
html.Append("<a target='_blank' href= " + "'" + row[dt.Columns["subCategoryURL"]] + "'" + " >");
html.Append(row[column.ColumnName]);
html.Append("</a>");
html.Append("</td>")
}
html.Append("</tr>");
}
I currently have the SP return 2 columns of information: subCategoryName, subCategoryDescription. I wanted to added a 3rd column subCategoryURL which would serve as an tag for a link as below:
<td>
<a target="_blank" href="#">Dealer Support</a>
</td>
<td>
<a target="_blank" href="#">Support information for dealer management</a>
</td>
How would I target each column specifically to build the table? I attempted to use html.Append(row[dt.Columns["subCategoryURL"]]);
but it did not return the desired result. What could I do next?
Upvotes: 0
Views: 511
Reputation: 108
To get an item of a specific Type (T) from a DataRow (datarow) corresponding to a specific column (columnName), you can use the following.
dataRow.Field<T>(columnName)
So,
string url = row.Field<string>(subCategoryURL)
may work.
Keep in mind that T must be of the same type as the data stored in the specified column of the DataRow. Should subCatergoryURL point to a row of doubles, the above would throw an InvalidCastException.
See here.
Upvotes: 1
Reputation: 169390
The subcategory URL ended up in the table...how would I target the other 2 to in the loop
The same way, i.e.:
foreach (DataRow row in dt.Rows)
{
html.Append("<tr>");
html.Append("<td>");
html.Append("<a target='_blank' href= " + "'" + row["subCategoryURL"].ToString() + "'" + " >");
html.Append(row["subCategoryName"].ToString());
html.Append("</a>");
html.Append("</td>");
html.Append("<td>");
html.Append(row["subCategoryDescription"].ToString());
html.Append("</td>");
html.Append("</tr>");
}
row["subCategoryName"] will get you the value of the "subCategoryName" column of the current row.
Upvotes: 1