I want to create columns in my DataGridView just like that:

______________________________________
|________________Taxes________________|
|_______Tax 1_______|______Tax 2______|
| 3,50 | 4,50 |
| 6,60 | 4,22 |


But how do I make column headers with 2 levels, and merge the first
row?

Thanks.

Hi Avirag,

The code is short and simple, but it is not working in C#.Net

and I m using DataGridView.

Kindly help me.

Edited 6 Years Ago by Kavyashri: n/a

Which code doesnt work and in what way is it not working? The link avirag gave you was to a section of code written in C#.net

I m completely new to C#.Net, and I couldnt find " RowCreated" Event Handler for my DataGridView.

So, I created a new Event Handler, but now I dont know how to call it.

pls help me out... Below is my code ...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Web.UI.WebControls;



namespace DgvColumnHeaderMerge
{
public partial class DgvColumnHeaderMerge : Form
{
public event GridViewRowEventHandler RowCreated;

public DgvColumnHeaderMerge()
{

InitializeComponent();
}

private void DgvColumnHeaderMerge_Load(object sender, EventArgs e)
{
DgvColumnHeaderMerge d_obj = new DgvColumnHeaderMerge();

this.dataGridView2.Columns.Add("dept_name", "Name");

this.dataGridView2.Columns.Add("dept_code", "Code");

this.dataGridView2.Columns.Add("emp_name", "Name");

this.dataGridView2.Columns.Add("emp_place", "Place");

this.dataGridView2.Columns.Add("emp_phone", "Phone No");

// RowCreated(dataGridView2,e);

//dataGridView2.Rows.Add();


}

void dataGridView2_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
//Build custom header.
GridView oGridView = (GridView)sender;

//DataGridView oGridView = (DataGridView)sender;
GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell oTableCell = new TableCell();

//Add Department
oTableCell.Text = "Department";
oTableCell.ColumnSpan = 2;
oGridViewRow.Cells.Add(oTableCell);

//Add Employee
oTableCell = new TableCell();
oTableCell.Text = "Employee";
oTableCell.ColumnSpan = 3;
oGridViewRow.Cells.Add(oTableCell);
oGridView.Controls[0].Controls.AddAt(0, oGridViewRow);


}
} 




}
}

I want the output as following,

---------------------------------------------------------------
Department | Employee |
---------------------------------------------------------------
Name | Code | Name | Place | Phone No |
---------------------------------------------------------------
| | | | |
---------------------------------------------------------------
| | | | |
---------------------------------------------------------------


Kindly correct my mistakes.

Thanks and Regards,

Kavya Shri
Thread: Merging headers in datagridview of C#.net
Forum: C#

Edited 6 Years Ago by peter_budo: Keep It Organized -For easy readability, always wrap programming code within posts in [code] (code blocks)

Hi Ryshad

there is no " RowCreated " Event under lightning bolt for DataGridView.

should i include any namespace to get RowCreated Event ????

Are you creating an ASP.NET or a Winforms project?
The RowAdded event and the ColumnSpan properties are members of the ASP.NET GridView control. There isnt a ColumnSpan property in the WinForm DataGridView control.

Hi Ryshad,

I m creating a Windows Form project and am using DataGridView in C#.

How should I do now ???

I did something similar recently, but i used two seperate tables, one placed directly above the other to create the multicolumn headers. I've been meaning to condense it together into a custom control, i'll find some time today to take a look for you :)

If you wanna try it yourself, you just have to position the header table so that it sits over the main table, populate it any time you bind the main table and then handle the main tables ColumnWidthChanged and Scroll events to sync the columns.

Hi Ryshad,
tatz a nice idea, i ll tyr it out.
But pls let me know if u find any method to put them together.

This article has been dead for over six months. Start a new discussion instead.