Click here to Skip to main content
15,605,875 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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()

        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);



        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;

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

I want the output as following,
Department |       Employee           |
Name | Code | Name | Place | Phone No |
     |      |      |       |          |
     |      |      |       |          |

Kindly help me correct my mistakes.

Thanks and Regards,

Kavya Shri
Thread: Merging headers in datagridview of
Forum: C#
Updated 22-Dec-09 22:41pm

1 solution

Hey Kavya

I'm pretty sure the above code doesn't compile...

An event should be declared as type of delegate, the delegate then dictates what kind of parameters should by supplied when triggering the event and also which parameters are sent to the event handler.

Getting used to how events work is a bit tricky at first. I wrote an article on delegates and events Here[^] that explains it.

Hope this helps
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900