这里我将值添加到网格中。这是我的数据库表的连接。它没关系并且运行良好但我想通过除以txtOrderQuantity(文本框ID名称)值来显示我的Qty字段数据。
<asp:TextBox ID="txtOrderQuantity" runat="server"
ontextchanged="txtOrderQuantity_TextChanged" AutoPostBack="true"> </asp:TextBox>
如何通过用txtorderQuantity除以2来显示网格视图字段。
public DataTable GetData()
{
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],[BagSz]FROM [dbo].[RawMaterials]",con);
sa.SelectCommand = cmd;
sa.Fill(dt);
con.Close();
return dt;
}
答案 0 :(得分:1)
使用gridview的RowDataBound事件并执行以下步骤:
答案 1 :(得分:1)
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事件。当一个DataRow绑定在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) ;
}
}