将2列相乘并从asp表中生成总计

时间:2014-11-01 10:40:17

标签: c# asp.net .net ms-access

我正在开发简单的结算网站。数据库采用* .mdb格式的Access 2013。使用的语言是c#.net

现在, 我在Home.aspx文件中有一个表

<fieldset>
       <asp:Table ID="DisplayTable" runat="server" CssClass="pure-table-horizontal" align="center"></asp:Table>
    </fieldset>

此表在运行时通过以下代码在Home.aspx.cs

中进行Flooded
protected void Page_Load(object sender, EventArgs e)
{
    //Use a string variable to hold the ConnectionString.
    string connectString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=  C:\Users\Raunaq\Documents\Visual Studio 2013\WebSites\BookStore\Database\Database.mdb";
    //Create an OleDbConnection object, 
    //and then pass in the ConnectionString to the constructor.
    OleDbConnection cn = new OleDbConnection(connectString);

    //Open the connection.
    cn.Open();

    //Use a variable to hold the SQL statement.
    string selectString = "SELECT Book_ID, Book_Name, Cost FROM Book_Details";

    //Create an OleDbCommand object.
    //Notice that this line passes in the SQL statement and the OleDbConnection object
    OleDbCommand cmd = new OleDbCommand(selectString, cn);

    //Send the CommandText to the connection, and then build an OleDbDataReader.
    //Note: The OleDbDataReader is forward-only.
    OleDbDataReader reader = cmd.ExecuteReader();

    //Set a table width.
    DisplayTable.Width = Unit.Percentage(50.00);
    //Create a new row for adding a table heading.
    TableRow tableHeading = new TableRow();

    //Create and add the cells that contain the Customer ID column heading text.
    TableHeaderCell customerIDHeading = new TableHeaderCell();
    customerIDHeading.Text = "ID";
    customerIDHeading.HorizontalAlign = HorizontalAlign.Left;
    tableHeading.Cells.Add(customerIDHeading);

    //Create and add the cells that contain the Contact Name column heading text.
    TableHeaderCell contactNameHeading = new TableHeaderCell();
    contactNameHeading.Text = "Book Name";
    contactNameHeading.HorizontalAlign = HorizontalAlign.Left;
    tableHeading.Cells.Add(contactNameHeading);

    //Create and add the cells that contain the Phone column heading text.
    TableHeaderCell phoneHeading = new TableHeaderCell();
    phoneHeading.Text = "Price";
    phoneHeading.HorizontalAlign = HorizontalAlign.Left;
    tableHeading.Cells.Add(phoneHeading);

    TableHeaderCell quan = new TableHeaderCell();
    quan.Text = "Quantity";
    quan.HorizontalAlign = HorizontalAlign.Left;
    tableHeading.Cells.Add(quan);


    DisplayTable.Rows.Add(tableHeading);

    //Loop through the resultant data selection and add the data value
    //for each respective column in the table.
    while (reader.Read())
    {
        TableRow detailsRow = new TableRow();
        TableCell customerIDCell = new TableCell();
        customerIDCell.Text = reader["Book_ID"].ToString();
        detailsRow.Cells.Add(customerIDCell);

        TableCell contactNameCell = new TableCell();
        contactNameCell.Text = reader["Book_Name"].ToString();
        detailsRow.Cells.Add(contactNameCell);

        TableCell phoneCell = new TableCell();
        phoneCell.Text = reader["Cost"].ToString() + " " + "&#8377;";
        detailsRow.Cells.Add(phoneCell);

        TableCell quanti = new TableCell();
        TextBox tb = new TextBox();
        tb.ID = reader["Book_ID"].ToString();
        tb.Text = "0";
        tb.Width = 66;
        quanti.Controls.Add(tb);
        detailsRow.Cells.Add(quanti);

        DisplayTable.Rows.Add(detailsRow);

    }

    //Close the reader and the related connection.
    reader.Close();
    cn.Close();
    Panel1.Visible = false;
}

现在我有一个名为&#39;计算&#39;的按钮。我想根据价格和数量列生成总数。 我尝试了很多东西,但到现在为止似乎没什么用。

请帮助我。 请告诉我你是否还需要其他任何东西来更好地理解我的问题。

  

谢谢。问题解决了。   溶液

 for (int i=1;i<row_count;i++)
    {
        TextBox tc = (TextBox)DisplayTable.FindControl("tb" + i);
        int q1 = Convert.ToInt32(tc.Text);
        TableCell tcc = (TableCell)DisplayTable.FindControl("cost" + i);
        char[] trimchar;
        string trim = " " + "&#8377;";
        trimchar = trim.ToCharArray();
        string a1 = tcc.Text.TrimEnd(trimchar);
        int a = Int32.Parse(a1);
        total_amt = q1 * a + total_amt;
    }
    Amount = total_amt;   
    Label1.Text = Amount.ToString() + "&#8377;";

2 个答案:

答案 0 :(得分:0)

我创建了一个MCVE示例。简而言之,为单个TableCell设置正确的ID,并使用FindControl获取与列对应的每个单元格值。

ASPX代码

<asp:Table ID="tbl" runat="server" />
<asp:Button ID="btn" runat="server" Text="Submit" OnClick="btn_Click" />

服务器端代码

protected void Page_Load(object sender, EventArgs e)
{
    BuildControls();
}
public void BuildControls()
{
    TableRow tr = new TableRow();
    TableCell tc = new TableCell();
    tc.Text = "hello world";
    tc.ID = "h1";
    tr.Cells.Add(tc);

    tc = new TableCell();
    tc.Text = "hello world1";
    tc.ID = "h2";
    tr.Cells.Add(tc);

    tbl.Rows.Add(tr);
}

protected void btn_Click(object sender, EventArgs e)
{
    TableCell c1 = (TableCell)tbl.FindControl("h1");
    string val1 = c1.Text;
    TableCell c2 = (TableCell)tbl.FindControl("h2");
    string val2 = c2.Text;
}

注意:我在服务器端代码中构建表,当我postback时,我使用FindControl获取值。

以下是此输出。

enter image description here

答案 1 :(得分:0)

将属性添加到数量单元格,如

  TableCell quanti = new TableCell();
    TextBox tb = new TextBox();
     quanti.Attributes.Add("data_qty","100");
    tb.ID = reader["Book_ID"].ToString();
    tb.Text = "100";
    tb.Width = 66;
    quanti.Controls.Add(tb);
    detailsRow.Cells.Add(quanti);

计算时,第一行是标题。所以,我们必须跳过它并计算表中的剩余行

    qty.Text = DisplayTable.Rows.Cast<TableRow>().ToList<TableRow>().Skip(1).Select(g => Convert.ToInt64(g.Cells[2].Text.Split(' ')[0].ToString()) * Convert.ToInt64(g.Cells[3].Attributes["data_qty"])).Sum().ToString();
相关问题