M
Marty McDonald
I have <asp:Table... </asp:Table> on my webform. In codebehind, I populate
a DataTable whose data should appear in the asp:Table. I created my own
code to populate the asp:Table with the DataTable, then I discovered the
asp:Table has a DataBind method. But the method takes no args and so I'm
confused how to use it. Is there a link to see how DataBind works for
asp:Table? Should I just use my own code anyway? Thanks...
Here's my code in case it helps answer any questions...
/// <summary>
/// Creates a Web Control Table from a DataTable.
/// </summary>
/// <param name="dataTable">DataTable containing data.</param>
/// <returns>Web Control Table with data from the DataTable.</returns>
public static System.Web.UI.WebControls.Table Map(System.Data.DataTable
dataTable)
{
System.Web.UI.WebControls.Table webTable = new
System.Web.UI.WebControls.Table();
foreach(System.Data.DataRow dataRow in dataTable.Rows)
{
System.Web.UI.WebControls.TableRow webRow = new
System.Web.UI.WebControls.TableRow();
foreach(System.Data.DataColumn dataColumn in dataTable.Columns)
{
System.Web.UI.WebControls.TableCell webCell = new
System.Web.UI.WebControls.TableCell();
Label label = new Label();
string colname = dataColumn.ColumnName;
label.Text = dataRow[colname].ToString();
webCell.Controls.Add(label);
webRow.Cells.Add(webCell);
}
webTable.Rows.Add(webRow);
}
return webTable;
}//end method
a DataTable whose data should appear in the asp:Table. I created my own
code to populate the asp:Table with the DataTable, then I discovered the
asp:Table has a DataBind method. But the method takes no args and so I'm
confused how to use it. Is there a link to see how DataBind works for
asp:Table? Should I just use my own code anyway? Thanks...
Here's my code in case it helps answer any questions...
/// <summary>
/// Creates a Web Control Table from a DataTable.
/// </summary>
/// <param name="dataTable">DataTable containing data.</param>
/// <returns>Web Control Table with data from the DataTable.</returns>
public static System.Web.UI.WebControls.Table Map(System.Data.DataTable
dataTable)
{
System.Web.UI.WebControls.Table webTable = new
System.Web.UI.WebControls.Table();
foreach(System.Data.DataRow dataRow in dataTable.Rows)
{
System.Web.UI.WebControls.TableRow webRow = new
System.Web.UI.WebControls.TableRow();
foreach(System.Data.DataColumn dataColumn in dataTable.Columns)
{
System.Web.UI.WebControls.TableCell webCell = new
System.Web.UI.WebControls.TableCell();
Label label = new Label();
string colname = dataColumn.ColumnName;
label.Text = dataRow[colname].ToString();
webCell.Controls.Add(label);
webRow.Cells.Add(webCell);
}
webTable.Rows.Add(webRow);
}
return webTable;
}//end method