C# 如何在gridview中显示计算值?

C# 如何在gridview中显示计算值?,c#,asp.net,database,C#,Asp.net,Database,在这里,我得到了我的网格值。这是我的数据库表的连接。这是正常的,工作正常,但我想通过除以我的txtOrderQuantity(textbox id name)值来显示我的数量字段数据 使用gridview的RowDataBound事件并执行以下步骤: 查找textbox控件及其值 找到MyQty值并将其与textbox值分开(在需要时使用特定类型强制转换) 找到控件以显示MyQty valud并分配计算值 你可以用两种方法 1) 可以将文本字段的值发送到GetData()方法。然后按以下方式准备

在这里,我得到了我的网格值。这是我的数据库表的连接。这是正常的,工作正常,但我想通过除以我的txtOrderQuantity(textbox id name)值来显示我的数量字段数据


使用gridview的RowDataBound事件并执行以下步骤:

  • 查找textbox控件及其值
  • 找到MyQty值并将其与textbox值分开(在需要时使用特定类型强制转换)
  • 找到控件以显示MyQty valud并分配计算值

  • 你可以用两种方法

    1) 可以将文本字段的值发送到GetData()方法。然后按以下方式准备select语句

    public DataTable GetData(int OrderQuantity)
        {
            DataTable dt = new DataTable();
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
            SqlDataAdapter sa = new SqlDataAdapter();
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT  [RmId],[RmName],[MeasuringUnit],[Rate],([Qty]/OrderQuantity )as Qty,[BagSz]FROM [dbo].[RawMaterials]",con);
            sa.SelectCommand = cmd;
            sa.Fill(dt);
            con.Close();
            return dt;
        }
    
    2) 您可以使用GridView的RowDataBound事件。当gridView中绑定了单个数据行时,就会发生此错误

    void YourGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
    
      if(e.Row.RowType == DataControlRowType.DataRow)
      {
        // Display the company name in italics.
       //  I assume the index of Qty column is 4
        e.Row.Cells[4].Text = decimal.Parse(e.Row.Cells[4].Text)/decimal.Parse(txtOrderQuantity.Text) ;
    
      }
    
    }
    

    您可以使用gridviewi用于数据绑定的DataBound事件此代码受保护的void txtOrderQuantity_TextChanged(对象发送方,EventArgs e){DataTable dt=new RawMaterialsProvider().GetData();gvTest.DataSource=dt;gvTest.DataBind();}否,这只是textbox的TextChange事件,@Chirag表示
    GridView
    的batabile事件!我没有使用数据绑定事件.e.Row.Cells[1].Text=decimal.Parse(e.Row.Cells[1].Text)/decimal.Parse(txtOrderQuantity.Text);无法将decimal隐式转换为string.e.Row.Cells[1]。Text=(decimal.Parse(e.Row.Cells[1].Text)/decimal.Parse(txtOrderQuantity.Text)).ToString();
    public DataTable GetData(int OrderQuantity)
        {
            DataTable dt = new DataTable();
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
            SqlDataAdapter sa = new SqlDataAdapter();
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT  [RmId],[RmName],[MeasuringUnit],[Rate],([Qty]/OrderQuantity )as Qty,[BagSz]FROM [dbo].[RawMaterials]",con);
            sa.SelectCommand = cmd;
            sa.Fill(dt);
            con.Close();
            return dt;
        }
    
    void YourGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
    
      if(e.Row.RowType == DataControlRowType.DataRow)
      {
        // Display the company name in italics.
       //  I assume the index of Qty column is 4
        e.Row.Cells[4].Text = decimal.Parse(e.Row.Cells[4].Text)/decimal.Parse(txtOrderQuantity.Text) ;
    
      }
    
    }