Calculate paid unpaid ASP.Net GridView Row count using C#

smile
 
on May 22, 2018 11:20 PM
1433 Views

https://i.imgur.com/Nns8DnS.gif

The code is not calculating total paid and total unpaid inside gridview.

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            CellPadding="4" ForeColor="#333333" GridLines="None"  Class="table table-striped table-bordered table-hover" 
            onrowcancelingedit="GridView1_RowCancelingEdit" 
            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" 
            onrowdatabound="GridView1_RowDataBound">
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
               <asp:TemplateField HeaderText="Invoice No.">
               <ItemTemplate >
                   <asp:Label ID="lbl_AdNo" runat="server" Text='<%# Eval("InvoiceNo") %>' ></asp:Label>
               </ItemTemplate>
               </asp:TemplateField>

                <asp:TemplateField HeaderText="Invoice Date">
                <ItemTemplate >
                    <asp:Label ID="lbl_Date" runat="server" Text='<%# Eval("InvoiceDate") %>'></asp:Label>
                </ItemTemplate>
               
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Grand Total">
                <ItemTemplate >
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("GrandTotal") %>'></asp:Label>
                </ItemTemplate>
               
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Customer Name">
                <ItemTemplate >
                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("CName") %>'></asp:Label>
                </ItemTemplate>
               
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Phone No.">
                <ItemTemplate >
                  <asp:Label ID="Label4" runat="server" Text='<%# Eval("CPhone") %>' ></asp:Label>
                </ItemTemplate>
                
                    </asp:TemplateField>
                                        
              
                <asp:TemplateField HeaderText="Status">
                <ItemTemplate >
                    <asp:Label ID="Label6" runat="server" Text='<%# Eval("Status") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate >
                     <asp:CheckBoxList ID="chkStatus" runat="server" s>
                         <asp:ListItem>Paid</asp:ListItem>
                         <asp:ListItem>Unpaid</asp:ListItem>
                     </asp:CheckBoxList>
                </EditItemTemplate>
                </asp:TemplateField>
                
                 <asp:TemplateField HeaderText="Edit" ShowHeader="false">
                    <ItemTemplate>
                        <asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" Text="Edit" ></asp:LinkButton>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" Text="Update" ></asp:LinkButton>
                        <asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                  </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#CCCCCC" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>

 

    private void BindGrid()
    {
        con = new SqlDbConnect();
        con.SqlQuery("select InvoiceNo,InvoiceDate,GrandTotal,CName,CPhone,Status from tblSales order by InvoiceNo Desc;");
        adapt.SelectCommand = con.Cmd;
        adapt.Fill(sTable);
        if (sTable.Rows.Count > 0)
        {
            GridView1.DataSource = sTable;
            GridView1.DataBind();
            lblTotal.Text = GridView1.Rows.Count.ToString();
            
            int Paid = 0;
            int Unpaid = 0;
         
            foreach (GridViewRow row in this.GridView1.Rows)
            {
                if (row.Cells[5].Text == "Paid")
                {
                    Paid++;
                }
                if (row.Cells[5].Text == "Unpaid")
                {
                    Unpaid++;
                }                
            }
            this.lblPaid.Text = Paid.ToString();
            this.lblUnpaid.Text = Unpaid.ToString();
        }
        else
        {
            Response.Write("No Record Found");
            return;
        }
        con.conClose();
    }
Download FREE API for Word, Excel and PDF in ASP.Net: Download
nagaraju60
 
on May 23, 2018 01:49 AM

Dear @smile,

You are Compare with "row.Cells[5].Text" with string but the cell is not boundfiled value. it is templatefield value so, first you find the label name and next compare that label text with string

please replace your bind() code with below lines of code.

private void BindGrid()
{
    con = new SqlDbConnect();
    con.SqlQuery("select InvoiceNo,InvoiceDate,GrandTotal,CName,CPhone,Status from tblSales order by InvoiceNo Desc;");
    adapt.SelectCommand = con.Cmd;
    adapt.Fill(sTable);
    if (sTable.Rows.Count > 0)
    {
        GridView1.DataSource = sTable;
        GridView1.DataBind();
        lblTotal.Text = GridView1.Rows.Count.ToString();
         
        int Paid = 0;
        int Unpaid = 0;
      
        foreach (GridViewRow row in this.GridView1.Rows)
        {
	   Label Label6 = row.Cells[5].FindControl("Label6") as Label;
		

            if (Label6.Text == "Paid")
            {
                Paid++;
            }
            if (Label6.Text == "Unpaid")
            {
                Unpaid++;
            }               
        }
        this.lblPaid.Text = Paid.ToString();
        this.lblUnpaid.Text = Unpaid.ToString();
    }
    else
    {
        Response.Write("No Record Found");
        return;
    }
    con.conClose();
}