0

データベースから選択したデータをテーブルに動的に追加する必要があります。そのために、ループで行が追加されたテーブルを作成しました。ただし、実際には最終行のみを追加します。ネーミングに関係あるのかな。以下は私が使用しているコードです。ヘッダーで正しくフォーマットされたテーブルを返しますが、データは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;
}
4

2 に答える 2

0

わかりましたので、この投稿のおかげで問題を解決しました。

私がしなければならなかったことは、ループ内に新しい行/セルを作成していることを確認することだけでした。私が何を意味するかを示すために、以下は現在機能しているループを示しています。

while (rdr.Read())
{
    DateTime temp = new DateTime();

    // This is the code that was added in.
    rid = new TableCell();
    rname = new TableCell();
    remail = new TableCell();
    rmobile = new TableCell();
    rdob = new TableCell();
    rstatus = new TableCell();
    rcheck = new TableCell();
    rview = new TableCell();
    redit = new TableCell();
    chk = new CheckBox();
    btnView = new Button();
    btnEdit = new Button();
    // Because I was creating new controls I also had to move my css classes to the loop as well
    rid.CssClass = "id";
    rname.CssClass = "name";
    remail.CssClass = "email";
    rmobile.CssClass = "mobile";
    rdob.CssClass = "dob";
    rstatus.CssClass = "status";
    rcheck.CssClass = "check";
    rview.CssClass = "button";
    redit.CssClass = "button";
    chk.Checked = false;
    btnView.Text = "View";
    btnEdit.Text = "Edit";

    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);

    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
    result.Rows.Add(row);
}
于 2013-06-10T10:52:50.473 に答える