customizing or mearging Header Row of DataGridView in C#.net

dotnet , csharp , datagridview India
  • 11 years ago

    Hi

    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
  • 11 years ago

    Hi

    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

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter” - Eric Raymond