kiran kamble Ответов: 1

Как сделать вертикальный расчет в gridview, несколько столбцов во время выполнения ?


        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing" AutoGenerateDeleteButton="true" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" Font-Names="KF-Kiran" ShowFooter="True" TabIndex="16" Width="90%" CellPadding="3" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" GridLines="Horizontal" DataSourceID="SqlDataSource1">
                    <%----%><%--DataSourceID="SqlDataSource1"--%>
                    <Columns>
                        <%--<asp:TemplateField HeaderText="vaaZ" 
                    SortExpression="Saves">
                    <ItemTemplate>
                      <asp:TextBox ID="txtIncrement" runat="server" Width="40px" Text='<%#Eval("Increment") %>'/>
                    </ItemTemplate>
                    <EditItemTemplate>
                         <asp:TextBox ID="txtIncrement" runat="server" Width="40px" Text='<%#Eval("Increment") %>'/>
                    </EditItemTemplate>
                    <%--<FooterTemplate>
                         <asp:TextBox ID="txtIncrement" runat="server" Width="40px"/>
                    </FooterTemplate>
             </asp:TemplateField>--%>
                        <%--<asp:TemplateField HeaderText="vaaZ" 
    SortExpression="Increment2">
    <ItemTemplate>
     <asp:Label ID="txtIncrement2" runat="server" Width="60px" Font-Bold="true" AutoPostBack="true" Text='<%#Eval("Increment2") %>'/>
    </ItemTemplate>
    <EditItemTemplate>
            <asp:Label ID="txtIncrement2" runat="server" Width="60px" Font-Bold="true" Text='<%#Eval("Increment2") %>'/>
    </EditItemTemplate>
    <FooterTemplate>
            <asp:Label ID="txtIncrement2" runat="server" Width="60px"/>
    </FooterTemplate>
</asp:TemplateField>--%>
                        <asp:TemplateField HeaderText="A.kx`." SortExpression="">
                            <ItemTemplate>
                                <b>
                                <asp:Label ID="lblSrNo" runat="server" ForeColor="#cc0000" Text="<%#Container.DataItemIndex+1 %>" />
                                </b>
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField HeaderText="kxamaacao naava" SortExpression="Work_Name">
                            <ItemTemplate>
                                <asp:Label ID="txtWork_Name" runat="server" Font-Bold="true" Text='<%#Eval("Work_Name") %>' Width="150px" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtWork_Name" runat="server" Font-Bold="true" Text='<%#Eval("Work_Name") %>' Width="150px" />
                            </EditItemTemplate>
                            <FooterTemplate>
                                <b style="font-family:Arial">Total Calculation:-</b>
                            </FooterTemplate>
                        </asp:TemplateField>

                        


                        <asp:TemplateField HeaderText="ToMxDr" SortExpression="Tender">
                            <ItemTemplate>
                                <asp:TextBox ID="txtTender" runat="server" OnTextChanged="txtTender_TextChanged1" min="1" max="150" MaxLength="3" AutoPostBack="true" Font-Bold="true" Text="0" Width="60px" />
                                <asp:RangeValidator ID="RangeValidator1" Font-Names="arial" runat="server" ErrorMessage="0-150 only" MinimumValue="1" MaximumValue="150" ControlToValidate="txtTender"></asp:RangeValidator>
                                <asp:Label ID="lblError" runat="server" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtTender" runat="server" Font-Bold="true" Width="60px" />
                            </EditItemTemplate>
                        </asp:TemplateField>




    <asp:TemplateField HeaderText="pauiva_ idlaa" SortExpression="Advance">
        <ItemTemplate>
            <asp:TextBox ID="txtAdvance" runat="server" Font-Bold="true" Text="0" Width="60px" />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtAdvance" runat="server" Font-Bold="true" Width="60px" />
        </EditItemTemplate>
    </asp:TemplateField>



                        <asp:TemplateField HeaderText="pa`tyaXa" SortExpression="Real_Val">
                            <ItemTemplate>
                                <asp:TextBox ID="txtReal_Val" runat="server" OnTextChanged="txtReal_Val_TextChanged1" AutoPostBack="true" Font-Bold="true" Text="0" Width="60px" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtReal_Val" runat="server" Font-Bold="true" Width="60px" />
                            </EditItemTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="bacata" SortExpression="Saves">
                            <ItemTemplate>
                                <asp:Label ID="txtSaves" runat="server" Font-Bold="true" Text="0.00" Width="60px" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:Label ID="txtSaves" runat="server" Font-Bold="true" Width="60px" Text='<%#Eval("Saves") %>' />
                            </EditItemTemplate>
                        </asp:TemplateField>
                       
            <asp:TemplateField HeaderText="vaaZ" SortExpression="Increment">
                    <ItemTemplate>
                      <asp:Label ID="txtIncrement" runat="server" Font-Bold="true" Width="40px" Text="0.00"/>
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:TextBox ID="txtIncrement" runat="server" Font-Bold="true" Width="40px" Text='<%#Eval("Increment") %>'/>
                    </EditItemTemplate>
             </asp:TemplateField>

                <asp:TemplateField HeaderText="dxr" SortExpression="Rate">
                    <ItemTemplate>
                        <span style="font-family:Arial;font-size:10px">Rs.</span>
                        <asp:Label ID="txtRate" runat="server" AutoPostBack="true" Font-Bold="true" style="font-family:KF-Kiran;" Text='<%#Eval("Rate") %>' Width="50px" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtRate" runat="server" Font-Bold="true" style="font-family:KF-Kiran;" Text='<%#Eval("Rate") %>' Width="50px" />
                    </EditItemTemplate>
                    <FooterTemplate>
                        <span style="font-family:Arial;font-size:15px">Rs.</span>
                        <asp:Label ID="lblTotalRate" runat="server" Font-Bold="true" Font-Size="X-Large" style="font-family:KF-Kiran" Width="50px" />
                    </FooterTemplate>
                </asp:TemplateField>


            <asp:TemplateField HeaderText="ToMxDr" SortExpression="Tender2">
                <ItemTemplate>
                    <asp:Label ID="txtTender2" runat="server" AutoPostBack="true" Font-Bold="true" Text="0.00" Width="60px" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID="txtTender2" runat="server" Font-Bold="true" onkeyup="CalculateTotals();" Width="60px" />
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="lblTotalTender2" runat="server" OnTextChanged="lblTotalTender2_TextChanged" Text="0.00" Font-Bold="true" Font-Names="KF-Kiran" />
                </FooterTemplate>
            </asp:TemplateField>



            <asp:TemplateField HeaderText="pa`tyaXa" SortExpression="Real_Val2">
                <ItemTemplate>
                    <asp:Label ID="txtReal_Val2" runat="server" AutoPostBack="true" Font-Bold="true" Text="0.00" Width="60px" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID="txtReal_Val2" runat="server" Font-Bold="true" Text='<%#Eval("Real_Val2") %>' Width="60px" />
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="lblTotalReal_Val" runat="server" Font-Bold="true" Font-Names="KF-Kiran" />
                </FooterTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="bacata" SortExpression="Saves2">
                <ItemTemplate>
                    <asp:Label ID="txtSaves2" runat="server" AutoPostBack="true" Font-Bold="true" Text="0.00" Width="60px" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID="txtSaves2" runat="server" Font-Bold="true" Text='<%#Eval("Saves2") %>' Width="60px" />
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="lblTotalSaves" runat="server" Font-Bold="true" Font-Names="KF-Kiran" />
                </FooterTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="vaaZ" SortExpression="Increment2">
                <ItemTemplate>
                    <asp:Label ID="txtIncrement2" runat="server" Width="60px" Font-Bold="true" AutoPostBack="true" Text="0.00"/>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID="txtIncrement2" runat="server" Width="60px" Font-Bold="true" Text='<%#Eval("Increment2") %>'/>
                </EditItemTemplate>
                <FooterTemplate>
                        <asp:Label ID="txtIncrement2" runat="server" Width="60px"/>
                </FooterTemplate>
                <FooterTemplate>
                    <asp:Label ID="lblTotalIncrement" runat="server" Font-Bold="true" Font-Names="KF-Kiran" />
                </FooterTemplate>
            </asp:TemplateField>

                        <asp:TemplateField HeaderStyle-Font-Names="arial" HeaderText="Action">
                            <ItemTemplate>
                                <asp:ImageButton ID="imgEdit" runat="server" CommandName="Edit" Height="20px" ImageUrl="~/Images/edit (9).png" Text="Edit" />
                                <asp:ImageButton ID="imgDelete" runat="server" CommandName="Delete" Height="20px" ImageUrl="~/Images/close (2).png" Text="Delete" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:ImageButton ID="imgUpdate" runat="server" CommandName="Update" Height="20px" ImageUrl="~/Images/Update.png" />
                                <asp:ImageButton ID="imgDelete" runat="server" CommandName="Cancel" Height="20px" ImageUrl="~/Images/Cancel.png" />
                            </EditItemTemplate>
                            <HeaderStyle Font-Names="arial" />
                        </asp:TemplateField>
                    </Columns>
                    <AlternatingRowStyle BackColor="#F7F7F7" />
                    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                    <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                    <SortedAscendingCellStyle BackColor="#F4F4FD" />
                    <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
                    <SortedDescendingCellStyle BackColor="#D8D8F0" />
                    <SortedDescendingHeaderStyle BackColor="#3E3277" />
                </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Road_PMCConnectionString %>" SelectCommand="SELECT [Work_Name], [Tender], [Real_Val], [Saves], [Increment], [Rate], [Tender2], [Real_Val2], [Saves2], [Increment2] FROM [tblIncrement_Details]"></asp:SqlDataSource>








Код CS
=======
/////--------Vertical Calculation
try
{
    var footlbl = GridView1.FooterRow.FindControl("lblTotalRate") as Label;
    if (footlbl != null)
    {
        footlbl.Text = stotal.ToString();
    }
}
catch { }






protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var ratelbl = e.Row.FindControl("txtRate") as Label;
            if (ratelbl != null)
            {
                stotal += Convert.ToDecimal(ratelbl.Text);
            }
        }
     }


Что я уже пробовал:

Я сделал всего один столбец, но когда я пытаюсь сделать больше одного столбца, это не сработает.

ZurdoDev

Почему не работает несколько столбцов? У вас есть код для создания одного столбца, поэтому несколько столбцов должны быть простыми. Просто найдите другой элемент управления и добавьте его в свой стотал. Простой. Где ты застрял?

kiran kamble

но другие поля пусты, поэтому его выбрасывание null reference exception, plz посмотрите на приведенный выше код и дизайн aspx и попробуйте сказать мне, как я могу это сделать?

1 Ответов

Рейтинг:
1

satyanarayana p.j

//After RowDataBound Below lines will loop through Gridview and you find individual Cell Values

foreach (GridViewRow row in GVInvoice.Rows)
{
   if (row.RowType == DataControlRowType.DataRow)
   {
     int index = Convert.ToInt32(row.RowIndex);
     decimal firstColumn = Convert.ToDecimal(GVInvoice.Rows[index].Cells[4].Text);
     decimal secondColumn =Convert.ToDecimal( GVInvoice.Rows[index].Cells[5].Text);
   }
}
//Here You can get cell value of each colummns, if you any control like label or text box, use find control then convert to decimal or int variable depending on your requirement.