Gridview with rows calcul
-
Hello I have a gridview with this form ID_student -------exam_mark1 ------exam_mark2-----average to calculate the average score I used this code
double average = 0; foreach (GridViewRow MRow in GridView1.Rows) { average = double.Parse(MRow.Cells[2].Text ) + double.Parse(MRow.Cells[3].Text); average = average / 2; MRow.Cells[4].Text = moyenne.ToString(); }
but it returns me the following error: The format of the input string is incorrect. Help me ,thanks -
Hello I have a gridview with this form ID_student -------exam_mark1 ------exam_mark2-----average to calculate the average score I used this code
double average = 0; foreach (GridViewRow MRow in GridView1.Rows) { average = double.Parse(MRow.Cells[2].Text ) + double.Parse(MRow.Cells[3].Text); average = average / 2; MRow.Cells[4].Text = moyenne.ToString(); }
but it returns me the following error: The format of the input string is incorrect. Help me ,thanksthe Cells collection is zero-based, so you want to average Cells(1) and Cells(2) adn put the answer in Cells(3), not 2, 3 adn 4 as you have it. But what a horrible way of doing things...on teh sassumption that these results are pulled from a database, why not pull the average directly out of that in your select statement? Or at least calculate the average in teh RowDataBound event....
-
Hello I have a gridview with this form ID_student -------exam_mark1 ------exam_mark2-----average to calculate the average score I used this code
double average = 0; foreach (GridViewRow MRow in GridView1.Rows) { average = double.Parse(MRow.Cells[2].Text ) + double.Parse(MRow.Cells[3].Text); average = average / 2; MRow.Cells[4].Text = moyenne.ToString(); }
but it returns me the following error: The format of the input string is incorrect. Help me ,thanksamina89 wrote:
but it returns me the following error: The format of the input string is incorrect.
Did you try to debug your code? Debugging if for this purpose only. As already suggested, better to do this calcaluation and assiging in
rowdatabound
event.Cheers!! Brij My Blog:http://brijbhushan.wordpress.com
Check my latest Article :ViewState - Various ways to reduce performance overhead -
amina89 wrote:
but it returns me the following error: The format of the input string is incorrect.
Did you try to debug your code? Debugging if for this purpose only. As already suggested, better to do this calcaluation and assiging in
rowdatabound
event.Cheers!! Brij My Blog:http://brijbhushan.wordpress.com
Check my latest Article :ViewState - Various ways to reduce performance overheadThanks all ; I still have the same error. I forgot to say that the gridview is in edit mode for columns -exam_mark1 ,-exam_mark2 and I do not know if this is the cause.
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderWidth="1px" CellPadding="4" CssClass="th" DataKeyNames="NUM_INSCRIPTION" Width ="60%" Height="9%" PagerStyle-CssClass="note" Style="position: absolute; left: 126px; top: 159px;" > <FooterStyle BackColor="White" ForeColor="#000066" /> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" CssClass="note" ForeColor="#000066" HorizontalAlign="Left" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> <Columns> <asp:TemplateField HeaderText="IDStudent"> <ItemStyle CssClass="td00" /> <ItemTemplate> <asp:Label ID="lblUserID1" runat="server" Text='<%# Eval("NUM_INSCRIPTION") %>'></asp:Label> <itemstyle cssclass="td00" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="StudentName"> <ItemStyle CssClass="td01" /> <ItemTemplate> <asp:Label ID="lblUserID2" runat="server" Text='<%# Eval("NOM_PRENOM_ETUDIANT_ARABE") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="mark1"> <ItemTemplate> <asp:Label ID="lblLastName1" runat="server" Text='<%# Eval("111") %>' Visible='<%# !(bool) IsInEditMode %>'></asp:Label> <asp:TextBox ID="moyt1" runat="server" Text='<%# Eval("111") %>' Visible='<%# IsInEditMode %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="mark2"> <ItemStyle CssClass="td02" /> <ItemTemplate>