Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
hi,
I have a gridview as
XML
<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>

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
C#
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
MIDL
sum+=Convert.ToInt16(GridView1.Rows[i].Cells[2].Text);

and so on.but still the same error.
Posted
Comments
Orcun Iyigun 8-Mar-11 12:03pm    
Have you tried debugging it? What is the value that you are getting from the cells?

Make sure you are not getting a null value because you can't convert a null value to a meaningful value through Convert. Your best option is to check for null first.

Another option;

Your example will not work if the object does not convert to a number. In "sum" do you only get numbers or a currency. for ex; $15 or something like that..
software_Engi08 8-Mar-11 12:11pm    
yes i have debug it.there is a null values in the cell. but y so .y i am getting null values .whats wrong
Orcun Iyigun 8-Mar-11 12:34pm    
do you have only null values or some value and null values? If you have only null values that means that you cant successfully bind. Check if your columns matches with your (query or dataset whatsoever). If you have null + values, that means the data that you are retrieving from have null values.

This line

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


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



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
Rajeev


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



 
Share this answer
 
 
Share this answer
 
 
Share this answer
 
v2
Comments
Rubaba 8-Mar-11 13:04pm    
best links!!
TweakBird 8-Mar-11 13:23pm    
Thank you.
vazid ali 16-Jun-12 8:41am    
int quant = 0;
HtmlInputText itQuant = (HtmlInputText)row.FindControl("itProductQuantity");
quant =int.Parse(itQuant.Value);

input string is not in correct formate please give me solution ...
i am beginer in asp.net
.thanks in advance

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