Sunday, August 11, 2019

How to group columns in gridview header row in ASP.NET (programmer friendly way)

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for fetch data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used one tables as below

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >


Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add a Webpage and Design for Show Data in Gridview With Group Column

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

HTML Code 
  1. <h3> Group columns in gridview header row in ASP.NET (programmer friendly way)</h3><br />
  2. <asp:GridView ID="GridView1" runat="server" CellPadding="5" AutoGenerateColumns="false" HeaderStyle-BackColor="#f3f3f3" BackColor="White">
  3. <Columns>
  4. <asp:BoundField HeaderText="Column 1" DataField="Column1" ControlStyle-CssClass="Group 1" />
  5. <asp:BoundField HeaderText="Column 2" DataField="Column2" ControlStyle-CssClass="Group 1" />
  6. <asp:BoundField HeaderText="Column 3" DataField="Column3" ControlStyle-CssClass="Group 1" />
  7. <asp:BoundField HeaderText="Column 4" DataField="Column4" ControlStyle-CssClass="Group 2" />
  8. <asp:BoundField HeaderText="Column 5" DataField="Column5" ControlStyle-CssClass="Group 2" />
  9. <asp:BoundField HeaderText="Column 6" DataField="Column6" />
  10. <asp:BoundField HeaderText="Column 7" DataField="Column7" />
  11. <asp:BoundField HeaderText="Column 8" DataField="Column8" />
  12. </Columns>
  13. </asp:GridView>

Step-6: Write following code in Page_Load event for Show data in Gridview With grouping column.


  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. PopulateData();
  6. }
  7. }
and here is the function

  1. private void PopulateData()
  2. {
  3. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  4. {
  5. GridView1.DataSource = dc.SampleDatas.ToList();
  6. GridView1.DataBind();
  7. }
  8. // Add Group Header
  9. SetGroupHeader();
  10. }
Additional this 2 function are required to Add group Columns header....
  1. private void SetGroupHeader()
  2. {
  3. int count = 1;
  4. string groupName = "";
  5. List<TableCell> headerCells = new List<TableCell>();
  6. if (GridView1.Rows.Count > 0)
  7. {
  8. foreach (DataControlField col in GridView1.Columns)
  9. {
  10. if (col.ControlStyle.CssClass != groupName)
  11. {
  12. if (groupName != "")
  13. {
  14. headerCells.Add(GetGroupHeader(groupName, count));
  15. count = 1;
  16. }
  17.  
  18. }
  19. else
  20. {
  21. count++;
  22. }
  23. groupName = col.ControlStyle.CssClass;
  24. }
  25. if (count > 1)
  26. {
  27. headerCells.Add(GetGroupHeader(groupName,count));
  28. }
  29. }
  30. if (headerCells.Count > 0)
  31. {
  32. GridViewRow gvrow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
  33. foreach (TableCell tc in headerCells)
  34. {
  35. gvrow.Cells.Add(tc);
  36. }
  37. GridView1.Controls[0].Controls.AddAt(0, gvrow);
  38. }
  39. }
  1. private TableCell GetGroupHeader(string headerText, int colSpan)
  2. {
  3. TableCell headerCell = new TableCell();
  4. headerCell.Text = headerText;
  5. headerCell.ColumnSpan = colSpan;
  6. return headerCell;
  7. }

Step-7: Run Application.



No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...