Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to display Gridview with Dynamic rows and column ..How can i achive this..please help me
Posted

1 solution

C#
public void BindOrders()
    {
        try
        {
            DataTable dtordernotobind = new DataTable();

            objFeedRetPL.cstype = Request.QueryString["customertype"].ToString();
            objFeedRetPL.statesno = Convert.ToInt32(Request.QueryString["state"].ToString());
            objFeedRetPL.customername = Request.QueryString["customername"].ToString();
            objFeedRetPL.fromdate = Convert.ToDateTime(Request.QueryString["fromdate"].ToString());
            objFeedRetPL.todate = Convert.ToDateTime(Request.QueryString["todate"].ToString());

            if (Request.QueryString["type"].ToString() == "1")
            {
                dtordernotobind = objFeedRetBAL.GetOrdernofalseReport(objFeedRetPL);
            }
            else
            {
                dtordernotobind = objFeedRetBAL.GetOrdernoTrueReport(objFeedRetPL);
            }
            if (dtordernotobind.Rows.Count > 0)
            {
                for (int i = 0; i < dtordernotobind.Rows.Count; i++)
                {
                    string customertype, customername, ordertype, frieght, orderdate;

                    string orderno;
                   

                    DataTable dtorderdetailstable = new DataTable();
                    objFeedRetPL.Orderno = dtordernotobind.Rows[i]["OrderNo"].ToString();

                    dtorderdetailstable = objFeedRetBAL.GetOrderTable(objFeedRetPL);

                    if (dtorderdetailstable.Rows.Count > 0)
                    {

                        orderno = dtorderdetailstable.Rows[0]["OrderNo"].ToString();
                        orderdate = dtorderdetailstable.Rows[0]["orderdate"].ToString();
                        customertype = dtorderdetailstable.Rows[0]["Customertype"].ToString();
                        customername = dtorderdetailstable.Rows[0]["Customername"].ToString();
                        ordertype = dtorderdetailstable.Rows[0]["Order Type"].ToString();

                        frieght = dtorderdetailstable.Rows[0]["freight"].ToString();

                        Table tbldynamic = new Table();
                        tbldynamic.BorderStyle = BorderStyle.Ridge;
                        tbldynamic.Width = 600;

                        TableCell tc = new TableCell();
                        TableRow tr = new TableRow();

                        Label lblorderno = new Label();
                        lblorderno.ID = "lblorderno" + i.ToString();
                        lblorderno.Text = "OrderNo:" + " ";
                        tc.Controls.Add(lblorderno);

                        Label lblordernodata = new Label();
                        lblordernodata.ID = "lblordernodata" + i.ToString();
                        lblordernodata.Text = orderno + "   ";

                        tc.Controls.Add(lblordernodata);
                        tr.Cells.Add(tc);
                        tbldynamic.Rows.Add(tr);

                        Label lbldate = new Label();
                        lbldate.ID = "lbldate" + i.ToString();
                        lbldate.Text = "Date:" + " ";
                        tc.Controls.Add(lbldate);

                        Label lbldatedata = new Label();
                        lbldatedata.ID = "lbldatedata" + i.ToString();
                        lbldatedata.Text = orderdate + "   ";

                        tc.Controls.Add(lbldatedata);
                        tr.Cells.Add(tc);
                        tbldynamic.Rows.Add(tr);

                        Label lblfreight = new Label();
                        lblfreight.ID = "lblFreight" + i.ToString();
                        lblfreight.Text = "Freight:" + " ";
                        tc.Controls.Add(lblfreight);

                        Label lblfreightdata = new Label();
                        lblfreightdata.ID = "lblfreightdata" + i.ToString();
                        lblfreightdata.Text = frieght + "   ";

                        tc.Controls.Add(lblfreightdata);
                        tr.Cells.Add(tc);
                        tbldynamic.Rows.Add(tr);

                        tc = new TableCell();
                        tr = new TableRow();

                        Label lblcstype = new Label();
                        lblcstype.ID = "lblcstype" + i.ToString();
                        lblcstype.Text = "Customer Type:" + " ";
                        tc.Controls.Add(lblcstype);

                        Label lblcstypedata = new Label();
                        lblcstypedata.ID = "lblcstypedata" + i.ToString();
                        lblcstypedata.Text = customertype + "   ";

                        tc.Controls.Add(lblcstypedata);
                        tr.Cells.Add(tc);
                        tbldynamic.Rows.Add(tr);

                        Label lblcsname = new Label();
                        lblcsname.ID = "lblcsname" + i.ToString();
                        lblcsname.Text = "Customer Name:" + " ";
                        tc.Controls.Add(lblcsname);

                        Label lblcsnamedata = new Label();
                        lblcsnamedata.ID = "lblcsnamedata" + i.ToString();
                        lblcsnamedata.Text = customername + "   ";

                        tc.Controls.Add(lblcsnamedata);
                        tr.Cells.Add(tc);
                        tbldynamic.Rows.Add(tr);

                        Label lblordertype = new Label();
                        lblordertype.ID = "lblordertype" + i.ToString(); ;
                        lblordertype.Text = " Order Type:" + " ";
                        tc.Controls.Add(lblordertype);

                        Label lblordertypedata = new Label();
                        lblordertypedata.ID = "lblordertypedata" + i.ToString();
                        lblordertypedata.Text = ordertype + "   ";

                        tc.Controls.Add(lblordertypedata);
                        tr.Cells.Add(tc);
                        tbldynamic.Rows.Add(tr);

                        DataTable dtorderdetails = new DataTable();
                        objFeedRetPL.Orderno = dtordernotobind.Rows[i]["OrderNo"].ToString();
                        objFeedRetPL.date = Convert.ToDateTime(dtorderdetailstable.Rows[0]["ordereddate"].ToString());
                        objFeedRetPL.statesno = Convert.ToInt32(dtordernotobind.Rows[0]["state"].ToString());

                        dtorderdetails = objFeedRetBAL.GetOrdersReport(objFeedRetPL);

                        GridView gvorderstally = new GridView();
                        gvorderstally.ID = "gvordertall" + dtordernotobind.Rows[i]["OrderNo"].ToString();
                        gvorderstally.Width = 600;
                        Label lblTotal = new Label();
                        Label lblwt = new Label();

                        BoundField FeedType = new BoundField();
                        FeedType.HeaderText = "FeedType";
                        FeedType.DataField = "Feedtype";
                        gvorderstally.Columns.Add(FeedType);

                        BoundField Bagtype = new BoundField();
                        Bagtype.HeaderText = "BagType";
                        Bagtype.DataField = "Bagtype";
                        gvorderstally.Columns.Add(Bagtype);

                        BoundField noofbags = new BoundField();
                        noofbags.HeaderText = "NoofBags";
                        noofbags.DataField = "noofbags";
                        gvorderstally.Columns.Add(noofbags);


                        BoundField rateperkg = new BoundField();
                        rateperkg.HeaderText = "Rate/Kg";
                        rateperkg.DataField = "rateperkg";
                        gvorderstally.Columns.Add(rateperkg);

                        BoundField totalamount = new BoundField();
                        totalamount.HeaderText = "Total Amount";
                        totalamount.DataField = "totalamount";
                        gvorderstally.Columns.Add(totalamount);

                        BoundField totalamountafterdiscount = new BoundField();
                        totalamountafterdiscount.HeaderText = "AfterDiscount";
                        totalamountafterdiscount.DataField = "AmountAfterDiscount";
                        gvorderstally.Columns.Add(totalamountafterdiscount);

                        gvorderstally.AutoGenerateColumns = false;
                        gvorderstally.ShowFooter = true;
                        gvorderstally.DataSource = dtorderdetails;
                        gvorderstally.DataBind();

                        divbirdtal.Controls.Add(tbldynamic);
                        divbirdtal.Controls.Add(gvorderstally);
                        divbirdtal.Controls.Add(lblTotal);
                        divbirdtal.Controls.Add(lblwt);

                        LiteralControl @break = default(LiteralControl);
                        @break = new LiteralControl("<br />");
                        divbirdtal.Controls.Add(@break);

                        LiteralControl @break1 = default(LiteralControl);
                        @break1 = new LiteralControl("<br />");
                        divbirdtal.Controls.Add(@break1);


                        GridViewRow footerRow = gvorderstally.FooterRow;
                        if (footerRow != null)
                        {
                            double totalSum = 0;
                            double totalamount1 = 0;
                            double afterdiscount = 0;
                            foreach (DataColumn c in dtorderdetails.Columns)
                            {
                                if (c.ColumnName.ToUpper() == "NOOFBAGS")
                                {
                                    double sum = 0;
                                    foreach (DataRow rw in dtorderdetails.Rows)
                                    {
                                        sum = sum + Convert.ToDouble(rw[c].ToString());
                                        footerRow.Cells[dtorderdetails.Columns.IndexOf(c.ColumnName)].Text = sum.ToString();
                                    }

                                    totalSum = Convert.ToDouble(totalSum + sum);
                                    totalSum = Convert.ToDouble(Math.Round(totalSum, 2));
                                }
                                if (c.ColumnName == "totalamount")
                                {
                                    double sum1 = 0;
                                    foreach (DataRow rw in dtorderdetails.Rows)
                                    {
                                        sum1 = sum1 + Convert.ToDouble(rw[c].ToString());
                                        footerRow.Cells[dtorderdetails.Columns.IndexOf(c.ColumnName)].Text = sum1.ToString();
                                    }

                                    totalamount1 = Convert.ToDouble(totalSum + sum1);
                                    totalamount1 = Convert.ToDouble(Math.Round(totalamount1, 2));
                                }
                                if (c.ColumnName == "AmountAfterDiscount")
                                {
                                    double sum2 = 0;
                                    foreach (DataRow rw in dtorderdetails.Rows)
                                    {
                                        sum2 =  sum2+ Convert.ToDouble(rw[c].ToString());
                                        //footerRow.Cells[dtorderdetails.Columns.IndexOf(c.ColumnName)].Text = sum2.ToString()+" + "+Convert.ToDouble(frieght).ToString()+" = ";
                                    }
                                    
                                    afterdiscount = Convert.ToDouble(sum2)+Convert.ToDouble(frieght);
                                    afterdiscount = Convert.ToDouble(Math.Round(afterdiscount, 2));
                                    if (Convert.ToDouble(frieght) != 0)
                                    {
                                        footerRow.Cells[dtorderdetails.Columns.IndexOf(c.ColumnName)].Text = sum2.ToString() + " + " + Convert.ToDouble(frieght).ToString() + " = " + afterdiscount;
                                    }
                                    else
                                    {
                                        footerRow.Cells[dtorderdetails.Columns.IndexOf(c.ColumnName)].Text = Convert.ToDouble(afterdiscount).ToString();
                                    }
                                    
                                }
                            }
                        }




                    }
                }
            }

        }
        
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900