Introduction
In many asp.net applications, we use the GridView control for displaying data. It is a very helpful control and powerful indeed.
Many operations can be done using the grid view control but the UI presentation is more important as it is the look-n-feel that matters a lot.
In this small article, I will demonstrate how the cells of the grid can be merged as I have been asked to do the same in one of my recent projects.
Application & Program overview
There may be other ways of implementing the problem but I am presenting the way which I adopted to solve the problem.
In the sample included here, I have created a GridView in the CellMerging.aspx while the datasouce is a datatable included in the DataSource.cs file located in the App_Code folder.
The source table is given below
{
DataTable dtsource = new DataTable();
//Adding columns to datatable
dtsource.Columns.Add("UserId");
dtsource.Columns.Add("Name");
dtsource.Columns.Add("Age");
dtsource.Columns.Add("Address");
dtsource.Columns.Add("Sex");
dtsource.Columns.Add("DOB");
dtsource.Columns.Add("Year");
dtsource.Columns.Add("Designation");
//Adding records to the datatable
dtsource.Rows.Add("1", "Name1", "20", "Address1", "Male", "20/12/1978", "1978","Student");
dtsource.Rows.Add("2", "Name2", "21", "Address2", "Male", "20/12/1984", "1984", "Doctor");
dtsource.Rows.Add("3", "Name3", "22", "Address3", "Male", "10/11/1943", "1943", "Software Eng.");
dtsource.Rows.Add("4", "Name4", "23", "Address4", "Male", "12/12/1956", "1956", "Police man");
dtsource.Rows.Add("5", "Name5", "24", "Address5", "Male", "11/01/1856", "1856", "CEO");
dtsource.Rows.Add("6", "Name6", "25", "Address6", "Female", "29/12/2008", "2008", "Manager");
dtsource.Rows.Add("7", "Name7", "26", "Address7", "Female", "29/12/2004", "2004", "Business man");
dtsource.Rows.Add("8", "Name8", "27", "Address8", "Female", "29/12/1956", "1956", "Student");
dtsource.Rows.Add("9", "Name9", "28", "Address9", "Female", "15/10/2009", "2009", "Project Manager");
dtsource.Rows.Add("10", "Name10", "29", "Address10", "Female", "29/12/2010", "2010", "Doctor");
return dtsource;
}
public DataTable MyDataTable()
In the Page_Load event I have populated the grid from the datasource defined above
{ DataSource objdatasource = new DataSource(); grMergedCellExample.DataSource = objdatasource.MyDataTable(); grMergedCellExample.DataBind(); } |

Rajendra TripathyPosted Sep 29, 2010, 7:38 AM
Hi Niladri. Your article is helpful, but I need Cell Merging In WPF DataGrid (Windows Application C#). Will you Guide me in this? for more detail u can see- http://img709.imageshack.us/img709/6213/cellmerged.jpg