从gridview读取数据,然后追加到文本框

时间:2019-04-03 00:24:03

标签: c# asp.net sql-server visual-studio

如何从gridview读取数据?我想添加OrderLinePrice列的内容并追加到总订单价格文本框中吗?我希望当我单击“添加到订单”按钮(以更新总订单价格)时发生所有这些情况

我在想-'为每一行将每个OrderLinePrice列值加在一起'也许我可以将此公式合并到计算订单价格按钮中

“添加至订单”按钮的代码:

protected void AddToOrderBtn_Click(object sender, EventArgs e)
    {
        try
        {
            // this code will insert the data the user has inputted
            string connectionString;
            SqlConnection cnn;

            connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Greenwich_Butchers;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
            cnn = new SqlConnection(connectionString);

            cnn.Open();
            Response.Write("Connection Made");
            SqlCommand command;
            SqlDataAdapter adapter = new SqlDataAdapter();

            String sql = "";
            sql = "INSERT INTO[OrderLine](OrderLineID, OrderID, ProductID, OrderLineQuantity, OrderLinePrice) Values('" + Convert.ToInt32(OrderLineIDTB.Text) + "','" + Convert.ToInt32(OrderIDTB.Text) + "','" + Convert.ToInt32(ProductIDTB.Text) + "', '" + Convert.ToInt32(OrderLineQuantityTB.Text) + "', '" + Convert.ToDecimal(ProdPriceTB.Text)*Convert.ToInt32(OrderLineQuantityTB.Text) + "')";
            command = new SqlCommand(sql, cnn);
            adapter.InsertCommand = new SqlCommand(sql, cnn);
            adapter.InsertCommand.ExecuteNonQuery();

            command.Dispose();



            cnn.Close();
        }
        catch (Exception ex)
        {
            Response.Write("error" + ex.ToString());
        }

        string connectionString1;
        SqlConnection cnn1;

        connectionString1 = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Greenwich_Butchers;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
        cnn1 = new SqlConnection(connectionString1);
        string selectSql1 = "SELECT * FROM [OrderLine]";
        SqlCommand com1 = new SqlCommand(selectSql1, cnn1);

        try
        {
            cnn1.Open();

            using (SqlDataReader read = com1.ExecuteReader())
            {
                while (read.Read())
                {
                    int orderlineidmax = Convert.ToInt32(read["OrderLineID"]);
                    orderlineidmax++;
                    OrderLineIDTB.Text = orderlineidmax.ToString();
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("error" + ex.ToString());
        }
        finally
        {
            cnn1.Close();
        }


        try
        {
            SqlConnection cnn2 = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Greenwich_Butchers;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");

            cnn2.Open();
            SqlCommand command1 = new SqlCommand("SELECT * FROM OrderLine WHERE OrderID = ('" + Convert.ToInt32(OrderIDTB.Text) + "') ", cnn2);
            SqlDataReader reader = command1.ExecuteReader();
            OrderDetailsGridView.DataSource = reader;
            OrderDetailsGridView.DataBind();
            cnn2.Close();

        }
        catch (Exception ex)
        {
            Response.Write("error" + ex.ToString());
        }

    }

application

2 个答案:

答案 0 :(得分:0)

在此之后获得总计:

 OrderDetailsGridView.DataSource = reader;
 OrderDetailsGridView.DataBind();
 cnn2.Close();
 GetTotal();//Code added to your textbox

public void GetTotal()
    {
        double Total = 0;
        foreach (GridViewRow r in OrderDetailsGridView.Rows)
        {
            Total = Total + double.Parse(r.Cells[4].Text); // double check if this is the orderline price
        }
        YourTextBox.Text = Total.ToString();
    }

注意:此代码尚未经过测试。

答案 1 :(得分:0)

我通过修改其他问题找到了解决方案,答案参见Answer

“计算总费用”按钮的代码:

private decimal ordersubtotal;

    protected void CalcPriceBtn_Click(object sender, EventArgs e)
    {

        string connectionString1;
        SqlConnection cnn1;

        connectionString1 = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Greenwich_Butchers;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
        cnn1 = new SqlConnection(connectionString1);
        string selectSql1 = "SELECT * FROM [OrderLine] WHERE OrderID = ('" + Convert.ToInt32(OrderIDTB.Text) + "')";
        SqlCommand com1 = new SqlCommand(selectSql1, cnn1);
        ordersubtotal = 0.0M;
        try
        {
            cnn1.Open();


            using (SqlDataReader read = com1.ExecuteReader())
            {

                while (read.Read())
                {

                    decimal rowtotal = Convert.ToDecimal(read["OrderLinePrice"]);
                    ordersubtotal += rowtotal;

                }

            }
        }
        catch (Exception ex)
        {
            Response.Write("error" + ex.ToString());
        }
        finally
        {
            cnn1.Close();
            OrderPriceTB.Text = ordersubtotal.ToString();
            CreateOrderBtn.Visible = true;
        }
    }