Click to See Complete Forum and Search --> : CustomiZing Header Row of Datagridview in C#.Net


Kavya Shri
December 16th, 2009, 11:01 PM
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
Thread: Merging headers in datagridview of C#.net
Forum: C#

pmatah
December 30th, 2009, 03:01 PM
Have you checked out the RowsAdded event already?

Here is more info with sample code:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowsadded.aspx

memeloo
December 30th, 2009, 04:12 PM
wait a second. you are trying to use asp.net controls in a winforms application? :confused:

and where are the code tags? :sick: