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;
}
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(dataGridView1.SelectedCells[0].ColumnIndex, dataGridView1.SelectedCells[0].RowIndex));
}
}
}