Click here to Skip to main content
14,332,414 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a gridview like
-----------------------------------------
|    Sno    |    Amount     |   DCNo    |
-----------------------------------------
|     1     |     1500      |    888    |
|     2     |     1000      |    888    |
|     3     |     2500      |    666    |
|     4     |     1500      |    666    |
-----------------------------------------

Now I want to display output like this.. My out put based on ,whenever DCNo same then it calculate the total amount.Then it will be merged cells.
----------------------------------------
|  Sno     |    Amount     |   DCNo    |
----------------------------------------
|    1     |               |           |
|    2     |    2500       |    888    |
|---------------------------------------
|    3     |     4000      |           |
|    4     |               |    666    |
 ---------------------------------------

I tried code Mergining cell

protected void GVDCNoConfirm_RowDataBound(object sender, GridViewRowEventArgs e)
   {
    for (int rowIndex = GVDCNoConfirm.Rows.Count - 2;                                     rowIndex >= 0; rowIndex--)
           {
               GridViewRow gvRow = GVDCNoConfirm.Rows[rowIndex];
               GridViewRow gvPreviousRow = GVDCNoConfirm.Rows[rowIndex + 1];
               for (int cellCount = 0; cellCount < gvRow.Cells.Count;
                                                             cellCount++)
               {
                   if (gvRow.Cells[cellCount].Text ==
                                          gvPreviousRow.Cells[cellCount].Text)
                   {
                       if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
                       {
                           gvRow.Cells[cellCount].RowSpan = 2;
                       }
                       else
                       {
                           gvRow.Cells[cellCount].RowSpan =
                               gvPreviousRow.Cells[cellCount].RowSpan + 1;
                       }
                       gvPreviousRow.Cells[cellCount].Visible = false;
                   }
               }
           }
        }


Now i want to display output with merged cell sum like second grid..
How can i achive this?
Posted
Updated 26-Oct-14 23:12pm
v2
Comments
SRS(The Coder) 9-Sep-14 0:56am
   
Could not get your requirement :-

As in first case you sum up and displayed both the Amount and DCNo in second row second and third columns respectively
where as in second scenario you have Amount sum in first row second column and DCNo in second row third column.

Can you please clarify it bit properly ?
ramaraog96 9-Sep-14 1:11am
   
if i have more than 2 Rows Same DCNo then it automatically Total sum of Amount as in "Single Cell"
Gupta Poonam 9-Sep-14 2:11am
   
Can you please clearly define your second table...Rows in second table are inconsistent. You can use Improve Question widget to edit your question.
Sinisa Hajnal 9-Sep-14 2:27am
   
Where did the data come from? If from a database, you should do your summing and grouping in the query, that's what databases are good at.
Prasad Avunoori 9-Sep-14 7:10am
   
Why don't you do that stuff at database level?
SRS(The Coder) 10-Sep-14 8:58am
   
Please check with LAG & LEAD keywords introduced in SQL Server.
I think if you will implement these properly, you can get your expected results.
Maciej Los 27-Oct-14 5:10am
   
Good question! My virtual 5!
Raghav Panalkar 5-Jun-19 5:59am
   
Good Solution

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 2

I have another way, you can set the htmlencoding property of Sno column of your second grid to false and use the br tag

Your second grid code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
       <Columns>
           <asp:BoundField DataField="Sno" HeaderText="Sno" HtmlEncode="False" />
           <asp:BoundField DataField="Amount" HeaderText="Amount" />
           <asp:BoundField DataField="DcNo" HeaderText="DcNo" />
       </Columns>
   </asp:GridView>


code to bind the grid

//Assume dtblSource is the datasource of your first grid
            //Declare a data table for grouping
            DataTable dtblGroup = new DataTable();
            dtblGroup.Columns.Add("Sno", typeof(string));
            dtblGroup.Columns.Add("Amount", typeof(decimal));
            dtblGroup.Columns.Add("DcNo", typeof(int));
           
            //Get distinct DC nos to a datatable
            DataView dv = new DataView(dtblSource);
            DataTable dtblDistinct = dv.ToTable(true, "DcNo");
            string Sno;
            decimal amount;
            int dcno;
            //loop through distinct dc nos
            foreach (DataRow drDist in dtblDistinct.Rows)
            {
                Sno = string.Empty;
                amount = 0;
                dcno = (int)drDist["DcNo"];
                DataRow[] drws = dtblSource.Select("DcNo=" + dcno.ToString());
             
                //Append slno, and sum amount
                foreach (DataRow dr in drws)
                {
                    amount += (decimal)dr["Amount"];
                    Sno = Sno + (Sno == string.Empty ? "" : "<br>") + dr["Sno"].ToString();
                }
                dtblGroup.Rows.Add(new object[] { Sno, amount, dcno });
            }
            GridView1.DataSource = dtblGroup;
            GridView1.DataBind();
   
v2

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