Click here to Skip to main content
14,354,963 members
Rate this:
Please Sign up or sign in to vote.
I have a gridview in which has 6 columns i.e: PRODUCTS, RETAIL OUTLET, DATE, TIME, QUANTITY & AMOUNT and same as the column names in my database table.
The main is issue is when data is populated in the gridview everything works fine and perfect, I ONLY NEED TO IMPLEMENT THAT IF THE PRODUCT COLUMN HAS 'CNG' IN IT THEN THE QUANTITY COLUMN NAME SHOULD BE 'QUANTITY - Kg(s)' AND ELSE IF THE PRODUCT COLUMN HAS 'PETROL,DIESEL or HI-OCTANE' THEN IT SHOULD DISPLAY THE HEADER OF COLUMN QUANTITY TO 'QUANTITY - Ltr'

Please help in this issue.

I am creating a custom header and grandtotals and subtotals in the row_created and row_databound events.
And I know there must be a solution in one of those events as here is my code in the following:


using System;


public partial class view_rep : System.Web.UI.Page
{
    string strPreviousRowID = string.Empty;
    string productype = string.Empty;
    int intSubTotalIndex = 1;
    double dblSubTotalTotalBilled = 0;
    double dblGrandTotalTotalBilled = 0;
    string[] arr;
    int myr;
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }


//HERE IS THE MAIN SCIENCE..

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Label lblamount = (Label)e.Row.FindControl("lbl_Amount");

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            strPreviousRowID = DataBinder.Eval(e.Row.DataItem, "product").ToString();
            productype = DataBinder.Eval(e.Row.DataItem, "product").ToString();
            double dblTotalBilled = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "amount").ToString());
            dblSubTotalTotalBilled += dblTotalBilled;
            dblGrandTotalTotalBilled += dblTotalBilled;
        }
        if (e.Row.RowIndex >= 0)
        {
            double dblTotalBilled = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "amount").ToString());
            lblamount.Text = string.Format("{0:#,#0.00}", dblTotalBilled);
        }
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        bool IsTotalRowNeedToAdd = false;
        bool IsGrandTotalRowNeedtoAdd = false;
        if ((strPreviousRowID != string.Empty) && (DataBinder.Eval(e.Row.DataItem, "product") != null))
            if (strPreviousRowID != DataBinder.Eval(e.Row.DataItem, "product").ToString())
                IsTotalRowNeedToAdd = true;

        if ((strPreviousRowID != string.Empty) && (DataBinder.Eval(e.Row.DataItem, "product") == null))
        {
            IsTotalRowNeedToAdd = true;
            IsGrandTotalRowNeedtoAdd = true;
            intSubTotalIndex = 0;
        }
        if ((strPreviousRowID == string.Empty) && (DataBinder.Eval(e.Row.DataItem, "product") != null))
        {
            GridView grdViewProducts = (GridView)sender;

            GridViewRow SubTotalRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);

            TableCell HeaderCell = new TableCell();
            HeaderCell.Text = "<div>" + " Fuel Type: " + DataBinder.Eval(e.Row.DataItem, "product").ToString() + "</br>" +
                            " Card No: " + DataBinder.Eval(e.Row.DataItem, "card_no").ToString() +
                        "</div>";
            HeaderCell.ColumnSpan = 5;
            HeaderCell.CssClass = "GroupHeaderStyle";
            SubTotalRow.Cells.Add(HeaderCell);

            grdViewProducts.Controls[0].Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, SubTotalRow);
            intSubTotalIndex++;


        }
        if (IsTotalRowNeedToAdd)
        {
            GridView grdViewProducts = (GridView)sender;

            // Creating a Row 
            GridViewRow SubTotalRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);

            //Adding Total Cell  
            TableCell HeaderCell = new TableCell();
            HeaderCell.Text = "Sub Total";
            HeaderCell.ColumnSpan = 4; // For merging first, second row cells to one 
            HeaderCell.CssClass = "SubTotalRowStyle";
            SubTotalRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = string.Format("{0:#,##0.00}", dblSubTotalTotalBilled);
            HeaderCell.CssClass = "SubTotalRowStyle";
            SubTotalRow.Cells.Add(HeaderCell);

            grdViewProducts.Controls[0].Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, SubTotalRow);

            intSubTotalIndex++;

            // Repeating Header Row before every product detail.
            GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);

            HeaderCell = new TableCell();
            HeaderCell.Controls.Add(new Label { Text = "Retail Outlet" });
            HeaderCell.CssClass = "headergv";
            HeaderCell.Height = Unit.Parse("16px");
            row.Cells.Add(HeaderCell);
            HeaderCell = new TableCell();
            HeaderCell.Controls.Add(new Label { Text = "Date" });
            HeaderCell.CssClass = "headergv";
            HeaderCell.Height = Unit.Parse("16px");
            row.Cells.Add(HeaderCell);
            HeaderCell = new TableCell();
            HeaderCell.Controls.Add(new Label { Text = "Time" });
            HeaderCell.CssClass = "headergv";
            HeaderCell.Height = Unit.Parse("16px");
            row.Cells.Add(HeaderCell);
            HeaderCell = new TableCell();
            // HERE IS THE CONDITION APPLIED AND IT WORKS FINE.. BUT WHEN THE PRODUCT HAS
            // ONLY PETROL THEN IT IS SHOWING THE QUANTITY - Kg(s)....WANT TO MAKE IT DYNAMIC.
            if (strPreviousRowID == "PETROL" || strPreviousRowID != "CNG")
            {
                HeaderCell.Controls.Add(new Label { Text = "Quantity - Kg(s)" });
            }
            else
            {
                HeaderCell.Controls.Add(new Label { Text = "Quantity - Ltr(s)" });
            }
            // ***********************************
            HeaderCell.CssClass = "headergv";
            HeaderCell.Height = Unit.Parse("16px");
            row.Cells.Add(HeaderCell);
            HeaderCell = new TableCell();
            HeaderCell.Controls.Add(new Label { Text = "Amount" });
            HeaderCell.CssClass = "headergv";
            HeaderCell.Height = Unit.Parse("16px");
            row.Cells.Add(HeaderCell);
            grdViewProducts.Controls[0].Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, row);
            intSubTotalIndex++;

            // *************************

            if (DataBinder.Eval(e.Row.DataItem, "product") != null)
            {
                GridViewRow SubTotalRow1 = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);

                TableCell HeaderCell1 = new TableCell();
                HeaderCell1.Text = "<div>" + " Fuel Type: " + DataBinder.Eval(e.Row.DataItem, "product").ToString() + "</br>" +
                                " Card No: " + DataBinder.Eval(e.Row.DataItem, "card_no").ToString() +
                            "</div>";
                HeaderCell1.ColumnSpan = 5;
                HeaderCell1.CssClass = "GroupHeaderStyle";
                SubTotalRow1.Cells.Add(HeaderCell1);

                grdViewProducts.Controls[0].Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, SubTotalRow1);
                intSubTotalIndex++;
            }

            dblSubTotalTotalBilled = 0;
        }

        if (IsGrandTotalRowNeedtoAdd)
        {
            GridView grdViewProducts = (GridView)sender;

            GridViewRow GrandTotalRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);

            TableCell HeaderCell = new TableCell();
            HeaderCell.Text = "Grand Total";
            HeaderCell.HorizontalAlign = HorizontalAlign.Left;
            HeaderCell.ColumnSpan = 4; // For merging first, second row cells to one 
            HeaderCell.CssClass = "GrandTotalRowStyle";
            GrandTotalRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = string.Format("{0:#,##0.00}", dblGrandTotalTotalBilled);
            HeaderCell.CssClass = "GrandTotalRowStyle";
            GrandTotalRow.Cells.Add(HeaderCell);

            grdViewProducts.Controls[0].Controls.AddAt(e.Row.RowIndex, GrandTotalRow);
        }
    }
}

Posted
Updated 13-Oct-13 19:54pm
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

try switch loop

switch (QtyType)
    {
        case "CNG":
         //code for header Text
            break;
        case "Petrol":
        //code for header Text
            break;
        Default :
    //code for header Text
            break;
    }
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

You can replace the string comparison with a "contains" and use a switch-case construct .

var QtyType = //datarow value of your product column consists of the type;
if (string.Contains(QtyType,StringComparison.OrdinalIgnoreCase))
{
    switch (QtyType)
    {
        case "CNG":
	//Assign header Text	
            break;
        case "Petrol":
        //Assign header Text
            break;
        Default :
	//Assign header Text
            break;
    }
}


You can think of refactoring this to a seperate method in case there are more types.
Hope this helps.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100