データベースから選択したデータをテーブルに動的に追加する必要があります。そのために、ループで行が追加されたテーブルを作成しました。ただし、実際には最終行のみを追加します。ネーミングに関係あるのかな。以下は私が使用しているコードです。ヘッダーで正しくフォーマットされたテーブルを返しますが、データは1行しかありません...
protected Table listVolunteers()
{
// Query to get volunteer information
string query = "SELECT gvo_id, gvo_name, gvo_email, gvo_mobile_ph, gvo_dob, gvo_status FROM volunteer";
// Table will hold the following information on volunteers:
// ID, Name, Email, Mobile, DOB, Status
// As well as a check box to do mass delete, a view button and an edit button on each
Table result = new Table();
result.ID = "volunteers";
result.CssClass = "list";
result.CellSpacing = 0;
// Creater table header
TableHeaderRow header = new TableHeaderRow();
header.CssClass = "header";
TableHeaderCell id = new TableHeaderCell();
id.CssClass = "id";
id.Width = 10;
id.Text = "ID";
header.Cells.Add(id);
TableHeaderCell name = new TableHeaderCell();
name.CssClass = "name";
name.Text = "NAME";
header.Cells.Add(name);
TableHeaderCell email = new TableHeaderCell();
email.CssClass = "email";
email.Text = "EMAIL";
header.Cells.Add(email);
TableHeaderCell mobile = new TableHeaderCell();
mobile.CssClass = "mobile";
mobile.Width = 70;
mobile.Text = "MOBILE #";
header.Cells.Add(mobile);
TableHeaderCell dob = new TableHeaderCell();
dob.CssClass = "dob";
dob.Width = 50;
dob.Text = "D.O.B";
header.Cells.Add(dob);
TableHeaderCell status = new TableHeaderCell();
status.CssClass = "status";
status.Width = 50;
status.Text = "STAT";
header.Cells.Add(status);
TableHeaderCell check = new TableHeaderCell();
check.CssClass = "check";
check.Width = 13;
header.Cells.Add(check);
TableHeaderCell view = new TableHeaderCell();
view.CssClass = "button";
view.Width = 50;
header.Cells.Add(view);
TableHeaderCell edit = new TableHeaderCell();
edit.CssClass = "button";
edit.Width = 50;
header.Cells.Add(edit);
// Add header to table
result.Rows.Add(header);
//Declaration for following code
TableCell rid = new TableCell();
rid.CssClass = "id";
TableCell rname = new TableCell();
rname.CssClass = "name";
TableCell remail = new TableCell();
remail.CssClass = "email";
TableCell rmobile = new TableCell();
rmobile.CssClass = "mobile";
TableCell rdob = new TableCell();
rdob.CssClass = "dob";
TableCell rstatus = new TableCell();
rstatus.CssClass = "status";
TableCell rcheck = new TableCell();
rcheck.CssClass = "check";
TableCell rview = new TableCell();
rview.CssClass = "button";
TableCell redit = new TableCell();
redit.CssClass = "button";
CheckBox chk = new CheckBox();
chk.Checked = false;
Button btnView = new Button();
btnView.Text = "View";
Button btnEdit = new Button();
btnEdit.Text = "Edit";
string rowClass = "odd";
Vector<TableRow> rows = new Vector<TableRow>();
// Add volunteers to table
try
{
using (conn)
{
conn.Open();
cmd = new OleDbCommand(query, conn);
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
DateTime temp = new DateTime();
rid.Text = rdr.GetValue(0).ToString();
rname.Text = rdr.GetValue(1).ToString();
remail.Text = rdr.GetValue(2).ToString();
rmobile.Text = rdr.GetValue(3).ToString();
DateTime.TryParse(rdr.GetValue(4).ToString(), out temp);
rdob.Text = temp.ToString("dd/MM/yy");
rstatus.Text = rdr.GetValue(5).ToString();
chk.ID = rid.Text;
btnView.PostBackUrl = string.Format("/VolunteerView.aspx?ID={0}", rid.Text);
btnEdit.PostBackUrl = string.Format("/VolunteerEdit.aspx?ID={0}", rid.Text);
rcheck.Controls.Add(chk);
rview.Controls.Add(btnView);
redit.Controls.Add(btnEdit);
TableRow row = new TableRow();
if (rowClass == "even")
rowClass = "odd";
else
rowClass = "even";
row.CssClass = rowClass;
// Add cells to row
row.Cells.Add(rid);
row.Cells.Add(rname);
row.Cells.Add(remail);
row.Cells.Add(rmobile);
row.Cells.Add(rdob);
row.Cells.Add(rstatus);
row.Cells.Add(rcheck);
row.Cells.Add(rview);
row.Cells.Add(redit);
// Add row to table
rows.Add(row);
}
TableRow[] array = new TableRow[rows.Count];
for (int i = 0; i < rows.Count; i++)
{
array[i] = rows[i];
}
result.Rows.AddRange(array);
}
}
catch (Exception e)
{
TableCell error = new TableCell();
TableRow row = new TableRow();
error.ColumnSpan = 9;
error.CssClass = "error";
error.Text = "An error occurred while loading the volunteers";
row.Cells.Add(error);
result.Rows.Add(row);
}
finally
{
if (rdr != null)
rdr.Close();
}
if (conn != null)
conn.Close();
return result;
}