CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2009
    Posts
    1

    CustomiZing Header Row of Datagridview in C#.Net

    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#

  2. #2
    Join Date
    Jan 2009
    Posts
    12

    Re: CustomiZing Header Row of Datagridview in C#.Net

    Have you checked out the RowsAdded event already?

    Here is more info with sample code:
    http://msdn.microsoft.com/en-us/libr...rowsadded.aspx

  3. #3
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: CustomiZing Header Row of Datagridview in C#.Net

    wait a second. you are trying to use asp.net controls in a winforms application?

    and where are the code tags?
    Last edited by memeloo; December 30th, 2009 at 05:16 PM.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured