asp.net c#将多个表格添加到表中仅产生一行

时间:2013-06-10 10:13:13

标签: c# asp.net dynamic naming

我必须动态地将数据库中的数据选择添加到表中。为此,我创建了一个在循环中添加了行的表。然而,它只是真正添加了最后一行。我认为它与命名有关?下面是我正在使用的代码。它返回表格,使用标题正确格式化,但只有一行数据......

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

2 个答案:

答案 0 :(得分:1)

尝试使用ImportRow方法()

result.ImportRow(row);

您可以替换您执行以下操作的位置:

// Add row to table
rows.Add(row);

并使用ImportRow方法。这就是它的全部内容,你不需要进一步的代码。

答案 1 :(得分:0)

好的,所以我通过this post解决了这个问题。

我必须做的就是确保我在循环中创建一个新的行/单元格。为了向您展示我的意思,下面显示了我现在正在运行的循环。

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