Гридвью Сумма Колонны
Я пытаюсь суммировать столбцы в нижний колонтитул в Gridview.
Каждая строка Gridview имеет отдельную сумму для каждого столбца.
Код ошибки в строке "presum1 = Convert.ToDouble(TempTxt1.Text);":
Input string was not in a correct format.
Код aspx.cs:
double sum1 = 0;
double sum2 = 0;
double sum3 = 0;
double presum1 = 0;
double presum2 = 0;
double presum3 = 0;
foreach (GridViewRow GR in GridView1.Rows)
{
TextBox TempTxt1 = (TextBox)GR.FindControl("txt1");
TextBox TempTxt2 = (TextBox)GR.FindControl("txt2");
TextBox TempTxt3 = (TextBox)GR.FindControl("txt3");
presum1 = Convert.ToDouble(TempTxt1.Text);
presum2 = Convert.ToDouble(TempTxt2.Text);
presum3 = Convert.ToDouble(TempTxt3.Text);
sum1 += presum1;
sum2 += presum2;
sum3 += presum3;
}
TextBox FooterTemp1 = (TextBox)GridView1.FooterRow.FindControl("TextBoxFootercol1");
FooterTemp1.Text = sum1.ToString();
TextBox FooterTemp2 = (TextBox)GridView1.FooterRow.FindControl("TextBoxFootercol2");
FooterTemp2.Text = sum2.ToString();
TextBox FooterTemp3 = (TextBox)GridView1.FooterRow.FindControl("TextBoxFootercol3");
код aspx:
<asp:GridView ID="GridView1"
runat="server"
DataSourceID="SqlDataSource1"
AutoGenerateColumns="false"
DataKeyNames="idt"
showfooter="true">
<Columns>
<asp:BoundField DataField="idt" HeaderText="idt" Readonly="true" SortExpression="idt" />
<asp:BoundField DataField="datetime" HeaderText="datetime" SortExpression="datetime" />
<asp:TemplateField SortExpression="col1">
<EditItemTemplate>
<asp:textbox id="col1TextBox" text='<%#Eval("col1")%>' runat="server"/>
<br/>
<asp:requiredfieldvalidator id="col1Validator"
controltovalidate="col1TextBox"
display="Dynamic"
text="Please enter data in col1."
runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txt1" runat="server" Text='<%# Eval("col1") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBoxFootercol1" text="col1" runat="server" MaxLength="40" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="col2">
<ItemTemplate>
<asp:TextBox ID="txt2" runat="server" Text='<%# Eval("col2") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:textbox id="col2TextBox" text='<%#Eval("col2")%>' runat="server"/>
<br/>
<asp:requiredfieldvalidator id="col2Validator"
controltovalidate="col2TextBox"
display="Dynamic"
text="Please enter data in col2."
runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBoxFootercol2" text="col2" runat="server" MaxLength="40" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="col3">
<EditItemTemplate>
<asp:textbox id="col3TextBox" text='<%#Eval("col3")%>' runat="server"/>
<br/>
<asp:requiredfieldvalidator id="col3Validator"
controltovalidate="col3TextBox"
display="Dynamic"
text="Please enter data in col3."
runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txt3" runat="server" Text='<%# Eval("col3") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBoxFootercol3" text="col3" runat="server" MaxLength="40" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource
id="SqlDataSource1"
ConnectionString="<%$ ConnectionStrings:Total %>"
SelectCommand="SELECT * FROM [test];"
</asp:SqlDataSource>