在这里,我正在为我的网格获取值。这是我的数据库表的连接。没关系并且运行良好,但我想通过除以我的 txtOrderQuantity(文本框 ID 名称)值来显示我的 Qty 字段数据。
<asp:TextBox ID="txtOrderQuantity" runat="server"
ontextchanged="txtOrderQuantity_TextChanged" AutoPostBack="true"> </asp:TextBox>
我如何使用 txtoderQuantity 除以 2 来显示我的 GridView 字段。
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;
}
最佳答案
有两种方式。
1) 您可以将文本字段的值发送到您的 GetData() 方法。然后按以下方式准备您的选择语句
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 绑定(bind)到 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) ;
}
}
关于c# - 如何在gridview中显示计算值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21373955/