C#中,模拟DataGridView的CellClick事件

来源:互联网 发布:linux 关闭tomcat 编辑:程序博客网 时间:2024/04/28 02:46

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            dt.Columns.Add("Age");
            for (int i = 0; i < 5; i++)
            {
                dt.Rows.Add();
                dt.Rows[i][0] = i;
                dt.Rows[i][1] = i.ToString() + "Name";
                dt.Rows[i][2] = (i + 1) * 5;
            }


            dataGridView1.DataSource = dt;
        }


        //第一个参数是第几列的意思,第二个参数是第几行的意思。  
        //所有的参数是以0开始的。 
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            MessageBox.Show(e.RowIndex.ToString() + ":" + e.ColumnIndex.ToString());
        }


        private void button1_Click(object sender, EventArgs e)
        {
            //dataGridView1_CellClick(dataGridView1, new DataGridViewCellEventArgs(1, 2));
            dataGridView1_CellClick(dataGridView1, new DataGridViewCellEventArgs(dataGridView1.SelectedCells[0].ColumnIndex, dataGridView1.SelectedCells[0].RowIndex));
        }
    }
}


参考:http://blog.csdn.net/dogfish/article/details/7002712

原创粉丝点击