Tuesday, 21 August 2018

Gridview Row merging

Gridview rows and column merging in asp.net



Public void Grid_bind()
{
   DataTable dt  = new DataTable();
   dt = obj_Da_get_data();
   Grid_sales_Details.DataSource = dt;
   Grid_sales_Details.DataBind();
   MergeRows(Grid_sales_Details);
}

 protected void gridView_PreRender(object sender, EventArgs e)
   {
            MergeRows(Grid_sales_Details);
   }

 public static 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];

                    for (int i = 0; i < 13; i++)
                    {
                        if (row.Cells[i].Text == previousRow.Cells[i].Text)
                        {
                            row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 :
                                                   previousRow.Cells[i].RowSpan + 1;
                            previousRow.Cells[i].Visible = false;
                        }
                    }
                    for (int i = row.Cells.Count - 2; i < row.Cells.Count; i++)
                    {
                        if (row.Cells[i].Text == previousRow.Cells[i].Text)
                        {
                            row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 :
                                                   previousRow.Cells[i].RowSpan + 1;
                            previousRow.Cells[i].Visible = false;
                        }
                    }
                }
        }

No comments:

Post a Comment

Top Agile Interview Questions & Answers

Top Agile Interview Questions & Answers 1. What is Agile Testing? The first question of agile interview question tests your k...