首页 > 解决方案 > 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;
        }

标签: c#asp.netdatabasegridview

解决方案


尝试这个

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;
    }  
}  

推荐阅读