locked
use cell merge for only the first column RRS feed

  • Question

  • User364607740 posted

    How can I do cell merge for common values in the first column only of DataTable? In this solution, it does the cell merge for every columns of the DataTable.

    protected void StdGrid_DataBound(object sender, EventArgs e)
    {
        for (int i = StdGrid.Rows.Count - 1; i > 0; i--)
        {
            GridViewRow row = StdGrid.Rows[i];
            GridViewRow previousRow = StdGrid.Rows[i - 1];
            for (int j = 0; j < row.Cells.Count; j++)
            {
                if (row.Cells[j].Text == previousRow.Cells[j].Text)
                {
                    if (previousRow.Cells[j].RowSpan == 0)
                    {
                        if (row.Cells[j].RowSpan == 0)
                        {
                            previousRow.Cells[j].RowSpan += 2;
                        }
                        else
                        {
                            previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
                        }
                        row.Cells[j].Visible = false;
                    }
                }
            }
        }
    }
    Student Name     Subjects      Marks
    S1      			ECO			81
    S1 					GEO			95
    S1 					FIN			98
    S2					ECO			98
    S2					FIN			98
    -------------------------------------------------------
    -------------------------------------------------------
    SAMPLE OUTPUT 
    Student Name     Subjects      Marks
    	      			ECO			81
    S1 					GEO			95
    					FIN			98
    -------------------------------------------
    					ECO			98
    S2					FIN			98
    -------------------------------------------


                               

    Friday, April 26, 2019 5:52 AM

All replies

  • User-893317190 posted

    Hi scala_1988 ,

    Just change j to  zero and remove the second loop.

    Below is my code.

     protected void StdGrid_DataBound(object sender, EventArgs e)
            {
                for (int i = StdGrid.Rows.Count - 1; i > 0; i--)
                {
                    GridViewRow row = StdGrid.Rows[i];
                    GridViewRow previousRow = StdGrid.Rows[i - 1];
    
                    if (row.Cells[0].Text == previousRow.Cells[0].Text)
                    {
                        if (previousRow.Cells[0].RowSpan == 0)
                        {
                            if (row.Cells[0].RowSpan == 0)
                            {
                                previousRow.Cells[0].RowSpan += 2;
                            }
                            else
                            {
                                previousRow.Cells[0].RowSpan = row.Cells[0].RowSpan + 1;
                            }
                            row.Cells[0].Visible = false;
                        }
                    }
    
                }
            }

    Best regards,

    Ackerly Xu

    Friday, April 26, 2019 8:44 AM