0

I have problems to get the data of a particular cell in my gridview i'm doing it this way :

double total = 0;

for (int i = 0; i < GridFactures.Rows.Count; i++)
    total += Convert.ToDouble(GridFactures.Rows[i].Cells[6].Text.ToString());

lblTotalTTC.Text = "Montant total TTC : " + total;

The column in question is declared in my aspx file :

<asp:TemplateField HeaderText="Montant TTC">
    <ItemTemplate>
        <asp:Label ID="lblMontantTTC" runat="server" Text='<%#Eval("MontantTTC") %>'/>
    </ItemTemplate>
</asp:TemplateField>

i'm sure this is always the sixth column i want to check. i put a break and GridFactures.Rows[i].Cells[6].Text.ToString()always contains "" nothing more ... thanks for your help

0

5 Answers 5

1

Instead of this code:

for (int i = 0; i < GridFactures.Rows.Count; i++)
    total += Convert.ToDouble(GridFactures.Rows[i].Cells[6].Text.ToString());

Try this:

for (int i = 0; i < GridFactures.Rows.Count; i++)
{
    Control ctrl = GridFactures.Rows[i].Cells[6].FindControl("lblMontantTTC");
    if (ctrl != null)
    {
        Label lbl = ctrl as Label;
        if (lbl != null)
        {
            total += Convert.ToDouble(lbl.Text);
        }
    } 
}
Sign up to request clarification or add additional context in comments.

Comments

0

If I'm not mistaken (and your label is the first/only control in the cell) -

You need to ask for the control at index 0, or find it by id, then ask for the .Text

Like so:

GridFactures.Rows[i].Cells[6].Controls[0].Text.ToString()

5 Comments

Unfortunately this is not working. I cannot manage to get any data from the gridview actually.
For testing purposes, try using the GridFactures.Rows[i].FindControl("lblMontantTTC") method and see if you can find it that way around.
thanks again but it's not working either... i don't understand
it displays { text = "40.01" } ( with a spy ) I need to get this value but i cannot get access to the Text Property for instance
WORKING WITH THIS : total = total + Convert.ToDouble(((System.Web.UI.WebControls.Label)(GridFactures.Rows[i].FindControl("lblMontantTTC"))).Text); thanks for your help
0

remember it is a zero based index, so [6] means cell 7

1 Comment

hehe a mistake that is made far too often just had to be sure ;)
0

I know why. I just ran into the same situation myself. You need to check fields that are empty, if so, convert it to zero first.

   total += (GridFactures.Rows[i].Cells[6].Text == "")? 0d : Convert.ToDouble(GridFactures.Rows[i].Cells[6].Text.ToString());

Comments

0

How about use LINQ instead of looping?

double mySum = 
        GridFactures.Rows
                    .Cast<GridViewRows>()
                    .Sum(row =>  
                Double.Parse(((Label)row.FindControl("lblMontantTTC")).Text)); 

If you're seeing no values, then your grid actually hasn't been databound yet, or ViewState is disabled on the grid. This all depends on where/when you're performing this calculation in the page lifecycle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.