本文介绍了如何找到GridView单元格文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个
gridview


I have a gridview as

<asp:GridView ID="GridView1" runat="server" Style="z-index: 114; left: 106px; position: absolute;
            top: 239px" Width="939px" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333">
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <Columns>

                <asp:TemplateField HeaderText="productname">
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("Product") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="price">
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("Price") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="quantity">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Quantity") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="total">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Total") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <EditRowStyle BackColor="#999999" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>


我想计算gridview中的所有行,也想添加所有总数,然后分配给文本框.
为此,我将代码编写为


and i want to count all the row in the gridview also want to add all the total and then assign to the textbox.
for this i have written code as

public void grandtotal()
    {
        int sum = 0;
        myDataTable = (DataTable)ViewState["PRODUCTTABLE"];
        for (int i = 0; i <=GridView1.Rows.Count;i++)
        {
            sum+=Convert.ToInt16(GridView1.Rows[i].Cells[3].Text);

        }
        TextBox7.Text = Convert.ToString(sum);

    }



但由于输入字符串的格式不正确,我遇到了错误.为什么.
上面的代码有什么问题.我也尝试过



but i am getting error as input string was not in a correct format .why so.
what is the wrong in the above code.also i tried as

sum+=Convert.ToInt16(GridView1.Rows[i].Cells[2].Text);


等等,但仍然是相同的错误.


and so on.but still the same error.

推荐答案

sum+ = Convert.ToInt16(GridView1.Rows[i].Cells[2].Text);



无法使用,因为数据不在您的gridview的单元格中.您正在使用模板字段,并希望从那里访问数据.
将以上行替换为-



is not working, as the data is not in your gridview''s cell.You are using the template fields and want to access the data from there.
Replace the above line with -

sum+ = Convert.ToInt16(((Label)GridView1.Rows[i].Cells[2].FindControl("Label1")).Text);




我还建议您使用gridview的RowDataBound事件处理程序作为替代.
在那里,您只需要将其调整为-




I would also suggest you to use the RowDataBound event handler of the gridview as an alternative.
There you will just need to tweak it as -

sum+ = Convert.ToInt16(((Label)e.Row.Cells[2].FindControl("Label1")).Text);




HTH
拉杰夫


如果有帮助,请投票并标记答案为可接受.






HTH
Rajeev


Please vote and mark the answer as accepted if this helps.






这篇关于如何找到GridView单元格文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:12