c# - gridview 列之间的计算
问题描述
我的项目中有一个文本框、一个按钮、一个 gridview 和一个 database.mdb。
gridview 根据写入文本框中的字符串显示来自 database.mdb 的结果。gridview 有两列,第一列包含字符串(与文本框相同),第二列包含数字。
我想从第二列中获取数字并在第三列中显示数学计算结果。可能吗?
例如:假设 100 是我在第二列中的号码,而 100-100*25/100 (xx*25/100) 的结果为“75”将自动显示在第三列中。
数据库连接和button_click代码:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/database.mdb"
SelectCommand="SELECT [Product], [Price] FROM [GENERAL] WHERE ([Product] = ?)">
<SelectParameters>
<asp:Parameter Name="Product" Type="String" />
</SelectParameters>
</asp:AccessDataSource>
<asp:GridView ID="GridView1" runat="server" HorizontalAlign="Center" AllowPaging="True" AutoGenerateColumns="False"
CellPadding="4" DataSourceID="AccessDataSource1" ForeColor="#333333"
GridLines="None" Width="700px" Height="100px" EnableModelValidation="True">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" HorizontalAlign="Center" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px"/>
<Columns>
<asp:BoundField DataField="Product" HeaderText="Product" SortExpression="Product" HeaderStyle-BorderColor="#CC9966" HeaderStyle-BorderWidth="1px" HeaderStyle-BorderStyle="Solid" >
<HeaderStyle BorderColor="#CC9966" BorderWidth="1px" BorderStyle="Solid"></HeaderStyle>
</asp:BoundField>
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" HeaderStyle-BorderColor="#CC9966" HeaderStyle-BorderWidth="1px" HeaderStyle-BorderStyle="Solid">
<HeaderStyle BorderColor="#CC9966" BorderWidth="1px" BorderStyle="Solid"></HeaderStyle>
</asp:BoundField>
</Columns>
{
string elist;
if (TextBox1.Text == "" || TextBox1.Text.Length < 5)
{
Label1.Visible = true;
GridView1.Visible = false;
}
else
{
elist = TextBox1.Text;
Label1.Visible = false;
GridView1.Visible = true;
AccessDataSource1.SelectParameters["Product"].DefaultValue = elist;
}
解决方案
尝试这个
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Checking the RowType of the Row
if(e.Row.RowType==DataControlRowType.DataRow)
{
int x=Convert.ToInt32(e.Row.Cells[1].Text);
Double calc= x-x*25/100//your formula for calculation
e.Row.Cells[2].Text=calc;
}
}
推荐阅读
- javascript - 未捕获的类型错误:无法读取空错误控制台的属性“删除”
- jquery - 如何在 jquery 数据表的 div 中永久添加内容?
- sql - 当没有找到值而不是空的“值”时,SQL 返回空的 JSON 括号
- r - 查找 NA 介于 0 和 1 之间的行
- vmware - 在调度程序过滤器 openstack 之上添加数据存储过滤器
- javascript - 从嵌套对象数组返回类似文件夹的路径
- wordpress - Polylang wordpress:从前端创建帖子
- java - kotlin编译器在编译kotlin代码时如何处理java代码?
- wordpress - WooCommerce 自定义结帐运输信息未保存问题
- vba - 将excel表格作为附件发送