hi guys in this article i am going to show you how to merge rows in grid view it two rows has same value.
idea is very simple to check if next and previous rows has some equal data then merge that number of rows.using col span.
lets have a look.
Now here there is no mathing value now m entering one matching value.
now here is name and number is same for dev so after click on merge button we will merge both row in same row and other will in different column.
here is code
protected void btnExport_Click(Object sender, EventArgs e)
{
MergeRows(grddata);
}
public void MergeRows(GridView gridView)
{
for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = gridView.Rows[rowIndex];
GridViewRow previousRow = gridView.Rows[rowIndex + 1];
if (row.Cells[0].Text == previousRow.Cells[0].Text && row.Cells[1].Text == previousRow.Cells[1].Text)
{
row.Cells[0].RowSpan = (previousRow.Cells[0].RowSpan < 2) ? 2 : previousRow.Cells[0].RowSpan + 1;
previousRow.Cells[0].Visible = false;
row.Cells[1].RowSpan = (previousRow.Cells[1].RowSpan < 2) ? 2 : previousRow.Cells[1].RowSpan + 1;
previousRow.Cells[1].Visible = false;
}
}
}
idea is very simple to check if next and previous rows has some equal data then merge that number of rows.using col span.
lets have a look.
Now here there is no mathing value now m entering one matching value.
now here is name and number is same for dev so after click on merge button we will merge both row in same row and other will in different column.
here is code
protected void btnExport_Click(Object sender, EventArgs e)
{
MergeRows(grddata);
}
public void MergeRows(GridView gridView)
{
for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = gridView.Rows[rowIndex];
GridViewRow previousRow = gridView.Rows[rowIndex + 1];
if (row.Cells[0].Text == previousRow.Cells[0].Text && row.Cells[1].Text == previousRow.Cells[1].Text)
{
row.Cells[0].RowSpan = (previousRow.Cells[0].RowSpan < 2) ? 2 : previousRow.Cells[0].RowSpan + 1;
previousRow.Cells[0].Visible = false;
row.Cells[1].RowSpan = (previousRow.Cells[1].RowSpan < 2) ? 2 : previousRow.Cells[1].RowSpan + 1;
previousRow.Cells[1].Visible = false;
}
}
}
0 comments: