在C#.Net中自定义DataGridView的标题行 [英] Customizing Header Row of DataGridView in C#.Net

查看:113
本文介绍了在C#.Net中自定义DataGridView的标题行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我对C#.Net完全陌生,我无法为我的DataGridView找到"RowCreated"事件处理程序.

因此,我创建了一个新的事件处理程序,但是现在我不知道如何调用它.

请帮帮我...下面是我的代码...

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




请帮助我纠正错误.

谢谢和问候,

Kavya Shri
线程:合并C#.net的datagridview中的标头
论坛:C#




Kindly help me correct my mistakes.

Thanks and Regards,

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

推荐答案

嘿卡维娅

我很确定上面的代码不会编译...

应该将事件声明为委托类型,然后委托指示在触发事件时应提供什么样的参数,以及将哪些参数发送给事件处理程序.

首先,习惯事件的工作方式有些棘手.我写了一篇有关委托人和事件的文章此处 [
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


这篇关于在C#.Net中自定义DataGridView的标题行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆