Click here to Skip to main content
16,021,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
gridview:-

XML
<asp:GridView ID="gvchekvalue" runat="server" AutoGenerateColumns="False"
                                                      DataSourceID="SqlDataSource1" CellPadding="4"
                                                      ForeColor="#333333" GridLines="None" Width="512px" Height="119px">

                                            <AlternatingRowStyle BackColor="White" />

                                            <Columns>
                                                <asp:TemplateField>
                                                    <ItemTemplate>
                                                        <asp:CheckBox ID="chkItem" runat="server" />
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="ID">
                                                    <ItemTemplate>
                                                        <asp:Label ID="label2" Text='<%#Eval("Id") %>' runat="server" />
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Name">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lblname" Text='<%#Eval("Name") %>' runat="server" />
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Price">
                                                    <ItemTemplate>
                                                        <asp:Label ID="Label1" Text='<%#Eval("price") %>' runat="server" />
                                                    </ItemTemplate>
                                                </asp:TemplateField>

                                            </Columns>

                                            <EditRowStyle BackColor="#2461BF" />
                                            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                                            <RowStyle BackColor="#EFF3FB" />
                                            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                                            <SortedAscendingCellStyle BackColor="#F5F7FB" />
                                            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                                            <SortedDescendingCellStyle BackColor="#E9EBEF" />
                                            <SortedDescendingHeaderStyle BackColor="#4870BE" />

                                        </asp:GridView>



Code:---

C#
protected void Button3_Click(object sender, EventArgs e)
        {

            double sum = 0;


            foreach (GridViewRow gvr in gvchekvalue.Rows)
            {
                CheckBox cb = (CheckBox)gvr.FindControl("chkItem");
                if (cb.Checked)
                {
                    double amount = Convert.ToDouble(gvr.Cells[1].ToString().Trim());
                    sum += amount;
                }


                txtamount.Text = sum.ToString();
            }
         }


but error occure:-
Input string was not in a correct format.
please help me Thanks
Posted
Comments
Arjsrya 2-Jan-15 6:19am    
Did you check the value of "gvr.Cells[1].ToString().Trim()".

Try something like gvr.Cells[1].Text.Trim()

Hope this link will help you Read me

1 solution

You need to find the price label and convert that to double. Also, assign the sum to TextBox after the loop. Update your code as below.
C#
protected void Button3_Click(object sender, EventArgs e)
{
    double sum = 0;

    foreach (GridViewRow gvr in gvchekvalue.Rows)
    {
        CheckBox cb = (CheckBox)gvr.FindControl("chkItem");

        if (cb.Checked)
        {
            Label lblPrice = (Label)gvr.FindControl("Label1");
            double amount = Convert.ToDouble(lblPrice.Text);
            sum += amount;
        }
    }

    txtamount.Text = sum.ToString();
 }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900