locked
get second word total in gridiview RRS feed

  • Question

  • User-807418713 posted

    Hello

    This is my gridview data

    USA APPLE  50
    SINGAPORE APPLE 30
    INDIA APPLE 40
    LONDON APPLE 10
    CHINA MANGO 5
    PAKISTAN MANGO 10
    NEW ZELAND MANGO 20

    I want output like this below how to fetech record as below

    APPLE 130
    MANGO 35

    thanking you

    Tuesday, January 19, 2021 7:33 PM

Answers

  • User1535942433 posted

    Hi Gopi.MCA,

    Accroding to your description,I don't understand your requirment clearly.

    1.How do you get the total value?Using SQL query?Or using code behind?

    2.Where do you fill the total data? Do you have another empty gridview?

    I have created a demo that I get the total data using jquery and fill another empty gridview which I created.Just like this:

     <script src="Scripts/jquery-3.2.1.min.js"></script>
        <script>
            $(function () {
                var x = 0;
                var y = 0;
                    $('#<%=GridView1.ClientID %>').find('tr').not(":first").each(function () {
                        if ($(this).find("[id*=lbname]").text().indexOf("APPLE") > -1) {
                            x += Number($(this).find("[id*=lbnum]").text());
                        }
                        else if ($(this).find("[id*=lbname]").text().indexOf("MANGO") > -1){
                            y+= Number($(this).find("[id*=lbnum]").text());
                        }
                    })
                    $('#<%=GridView2.ClientID %>').find('tr:eq(1)').find("td:eq(0)").text("APPLE");
                    $('#<%=GridView2.ClientID %>').find('tr:eq(1)').find("td:eq(1)").text(x);
                    $('#<%=GridView2.ClientID %>').find('tr:eq(2)').find("td:eq(0)").text("MANGO");
                    $('#<%=GridView2.ClientID %>').find('tr:eq(2)').find("td:eq(1)").text(y);
               
            })
        </script>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                       <asp:TemplateField HeaderText="Name">
                           <ItemTemplate>
                               <asp:Label runat="server" ID="lbname" Text='<%#Eval("name") %>' ></asp:Label>
                           </ItemTemplate>
                       </asp:TemplateField>
                        <asp:TemplateField HeaderText="Num">
                           <ItemTemplate>
                               <asp:Label runat="server" ID="lbnum" Text='<%#Eval("num") %>' ></asp:Label>
                           </ItemTemplate>
                       </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:GridView ID="GridView2" runat="server">
                </asp:GridView>

    Code-Behind:

     protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from Test11";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "Test11");
                GridView1.DataSource = ds.Tables[0].DefaultView;
                GridView1.DataBind();
                conn.Close();
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                    DataTable dt = new DataTable();
    
                    DataRow dr = null;
                    DataRow dr2 = null;
                    dt.Columns.Add(new DataColumn("TName", typeof(string)));
                    dt.Columns.Add(new DataColumn("Total", typeof(string)));
                    dr = dt.NewRow();
                    dr2 = dt.NewRow();
                    dt.Rows.Add(dr);
                    dt.Rows.Add(dr2);
                    GridView2.DataSource = dt;
                    GridView2.DataBind();
                }
            }

    Result:

    If you have another requirment,you could tell us.It will help us to solve your problems.

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 20, 2021 3:26 AM

All replies

  • User475983607 posted

    Share DDL and DML code to create and load the data into temp tables.  Then share the code you designed to solve this problem. 

    Tuesday, January 19, 2021 8:04 PM
  • User-1716253493 posted
    SELECT        REVERSE(LEFT(REVERSE(NAME), CHARINDEX(' ', REVERSE(NAME)) - 1)) AS FruitName, SUM(AMOUNT) AS Total
    FROM            Table_Fruit
    GROUP BY REVERSE(LEFT(REVERSE(NAME), CHARINDEX(' ', REVERSE(NAME)) - 1))

    Wednesday, January 20, 2021 12:15 AM
  • User1535942433 posted

    Hi Gopi.MCA,

    Accroding to your description,I don't understand your requirment clearly.

    1.How do you get the total value?Using SQL query?Or using code behind?

    2.Where do you fill the total data? Do you have another empty gridview?

    I have created a demo that I get the total data using jquery and fill another empty gridview which I created.Just like this:

     <script src="Scripts/jquery-3.2.1.min.js"></script>
        <script>
            $(function () {
                var x = 0;
                var y = 0;
                    $('#<%=GridView1.ClientID %>').find('tr').not(":first").each(function () {
                        if ($(this).find("[id*=lbname]").text().indexOf("APPLE") > -1) {
                            x += Number($(this).find("[id*=lbnum]").text());
                        }
                        else if ($(this).find("[id*=lbname]").text().indexOf("MANGO") > -1){
                            y+= Number($(this).find("[id*=lbnum]").text());
                        }
                    })
                    $('#<%=GridView2.ClientID %>').find('tr:eq(1)').find("td:eq(0)").text("APPLE");
                    $('#<%=GridView2.ClientID %>').find('tr:eq(1)').find("td:eq(1)").text(x);
                    $('#<%=GridView2.ClientID %>').find('tr:eq(2)').find("td:eq(0)").text("MANGO");
                    $('#<%=GridView2.ClientID %>').find('tr:eq(2)').find("td:eq(1)").text(y);
               
            })
        </script>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                       <asp:TemplateField HeaderText="Name">
                           <ItemTemplate>
                               <asp:Label runat="server" ID="lbname" Text='<%#Eval("name") %>' ></asp:Label>
                           </ItemTemplate>
                       </asp:TemplateField>
                        <asp:TemplateField HeaderText="Num">
                           <ItemTemplate>
                               <asp:Label runat="server" ID="lbnum" Text='<%#Eval("num") %>' ></asp:Label>
                           </ItemTemplate>
                       </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:GridView ID="GridView2" runat="server">
                </asp:GridView>

    Code-Behind:

     protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from Test11";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "Test11");
                GridView1.DataSource = ds.Tables[0].DefaultView;
                GridView1.DataBind();
                conn.Close();
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                    DataTable dt = new DataTable();
    
                    DataRow dr = null;
                    DataRow dr2 = null;
                    dt.Columns.Add(new DataColumn("TName", typeof(string)));
                    dt.Columns.Add(new DataColumn("Total", typeof(string)));
                    dr = dt.NewRow();
                    dr2 = dt.NewRow();
                    dt.Rows.Add(dr);
                    dt.Rows.Add(dr2);
                    GridView2.DataSource = dt;
                    GridView2.DataBind();
                }
            }

    Result:

    If you have another requirment,you could tell us.It will help us to solve your problems.

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 20, 2021 3:26 AM
  • User-537278906 posted

    i want to share thread on homes in South West Mississauga.

    Wednesday, January 20, 2021 6:27 PM