1

I am having the data in datatable with the below format that i am trying to bind to the gridview (format -1)

 Id     col1    col2
 20      a     ac-1
 20      a     ac-2
 20      a     ac-3
 20      a     ac-4
 21      a     ac-1
 21      a     ac-2
 21      a     ac-3
 21      a     ac-4

after had databound i am getting gridview in below format (format- 2)

 Id     col1    col2
 20      a      ac-1
                ac-2
                ac-3
                ac-4
 21             ac-1
                ac-2
                ac-3
                ac-4

but I am looking for the below format of the data to be represent in gridview (Format-3)

   Id   col1    col2
   20     a     ac-1
                ac-2
                ac-3
                ac-4
   21     a     ac-1
                ac-2
                ac-3
                ac-4

and the below code is for Ondatabound event in grdiview

   for (int i = gvConversionGrid.Rows.Count - 1; i > 0; i--)
   {
        GridViewRow row = gvConversionGrid.Rows[i];
        GridViewRow previousRow = gvConversionGrid.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;
                }
            }
        }
   }

Is there any way to manipulate the gridview and to bring the format for the gridview data like as in format-3..

Would any one please help on this one query that would be very grateful to me .. Many thanks in advance

Update : I am looking for the below format image

enter image description here

VDWWD
  • 35,079
  • 22
  • 62
  • 79
Glory Raj
  • 17,397
  • 27
  • 100
  • 203
  • is it not possible at all , would any one have any suggestions on this merge rows .. – Glory Raj Dec 15 '17 at 07:41
  • Is format 1 from one Table? and should it bind to only one GridView?? –  Dec 15 '17 at 08:35
  • @Asif.Ali it is form single datatable only .... yes it is bind it to only one gridview ... – Glory Raj Dec 15 '17 at 08:35
  • col1 value is same for all rows right and only Id and col2 are differs ? –  Dec 15 '17 at 08:39
  • @Asif.Ali sorry i could not get you col1 value is same for all rows means ? – Glory Raj Dec 15 '17 at 08:50
  • I've found your problem's solution see here https://stackoverflow.com/questions/10058443/how-to-show-a-listview-inside-a-gridview-controls-item-template Good Luck! :) –  Dec 15 '17 at 09:22
  • @Asif.Ali sorry that gridview is using listview inside .. here i using only gridview.. – Glory Raj Dec 15 '17 at 09:24

2 Answers2

1

You could insert 2 hidden columns

 Id     col1    col2  groupindex  groupcount
 20      a     1      1         4
 20      a     2      2         4
 20      a     3      3         4
 20      a     4      4         4
 21      a     1      1         5
 21      a     2      2         5
 21      a     3      3         5
 21      a     4      4         5
 21      a     5      5         5

and set rowspan:

 void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow )
    {

        // set rowspan for the first row of group
        if (Convert.ToInt32(e.Row.Cells[3]) == 1)
        {
            var rowSpan = Convert.ToString(e.Row.Cells[4]);
            e.Row.Cells[0].Attributes.Add("rowspan", rowSpan);
            e.Row.Cells[1].Attributes.Add("rowspan", rowSpan);
        }
        else
        {
            e.Row.Cells[0].Visible = false;
            e.Row.Cells[1].Visible = false;
        }
    }
}
TriV
  • 5,118
  • 2
  • 10
  • 18
  • Thanks for suggestion .. where do i need to insert that group Index and group count and how do i populate that into gridview ... – Glory Raj Dec 15 '17 at 08:03
1

You can set the previous Id value to a varable declared outside the method and compare the current row to that in the OnRowDataBound event of the GridView.

string previousCellValue = "";

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the dataitem back to a row
        DataRowView row = e.Row.DataItem as DataRowView;

        //check if the current id matches the previous row
        if (previousCellValue == row["Id"].ToString())
        {
            //clear the first cell
            e.Row.Cells[0].Text = "";

            //apply column span
            e.Row.Cells[0].ColumnSpan = 2;
            e.Row.Cells[1].Visible = false;
        }
        else
        {
            previousCellValue = row["Id"].ToString();
        }
    }
}

Result:

enter image description here

UPDATE

string previousCellValue = "";
int previousCellCount = 1;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow and not the first row
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the dataitem back to a row
        DataRowView row = e.Row.DataItem as DataRowView;

        //check if the current id matches the previous row
        if (previousCellValue == row["Id"].ToString())
        {
            //count the number of same cells
            previousCellCount++;
        }
        else
        {
            //span the rows for the first two cells
            if (previousCellCount > 1)
            {
                GridView1.Rows[e.Row.RowIndex - previousCellCount].Cells[0].RowSpan = previousCellCount;
                GridView1.Rows[e.Row.RowIndex - previousCellCount].Cells[1].RowSpan = previousCellCount;

                //hide the other cells in the column
                for (int i = 1; i < previousCellCount; i++)
                {
                    GridView1.Rows[(e.Row.RowIndex - previousCellCount) + i].Cells[0].Visible = false;
                    GridView1.Rows[(e.Row.RowIndex - previousCellCount) + i].Cells[1].Visible = false;
                }
            }

            previousCellValue = row["Id"].ToString();
            previousCellCount = 1;
        }
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        //use the footer row to create spanning for the last rows if needed
        if (previousCellCount > 1)
        {
            GridView1.Rows[GridView1.Rows.Count - previousCellCount].Cells[0].RowSpan = previousCellCount;
            GridView1.Rows[GridView1.Rows.Count - previousCellCount].Cells[1].RowSpan = previousCellCount;

            //hide the other cells in the column
            for (int i = 1; i < previousCellCount; i++)
            {
                GridView1.Rows[(GridView1.Rows.Count - previousCellCount) + i].Cells[0].Visible = false;
                GridView1.Rows[(GridView1.Rows.Count - previousCellCount) + i].Cells[1].Visible = false;
            }
        }
    }
}

Result:

enter image description here

VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • i did that manipulation in onDataBound .. you have given the method in rowDataBound event right.. – Glory Raj Dec 15 '17 at 08:03
  • Yes, it is the OnRowDataBound event. – VDWWD Dec 15 '17 at 08:04
  • Still I am getting only single "a" in second column .. could you please suggest any changes has to be made – Glory Raj Dec 15 '17 at 08:28
  • I don't understand your last comment. My snippet will produce the exact result as you indicated. See added screenshot. – VDWWD Dec 15 '17 at 08:37
  • i have attached image in my question , could you please suggest any changes to get that image kind of grid.. – Glory Raj Dec 15 '17 at 08:46
  • You need the values in the same cell? Not each in their own like in my example? – VDWWD Dec 15 '17 at 08:55
  • no .. sorry my bad I forgot to put the lines in that just updated the image could you please look into it .. – Glory Raj Dec 15 '17 at 08:58
  • I've updated my answer. Basically you need some more complex code. You also need to repeat the rowspan code in the footer row to create spanning for the last row(s) if needed. PS this was more difficult that your [500 rep question](https://stackoverflow.com/questions/47713671/insert-table-in-a-single-cell-inside-repeater) ;) – VDWWD Dec 15 '17 at 09:52