15.3DAtagridview获取当前单元格及指定单元格内容
using System.Windows.Forms;
namespace _15._3DAtagridview获取当前单元格
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“companyDataSet.clerk”中。您可以根据需要移动或删除它。
this.clerkTableAdapter.Fill(this.companyDataSet.clerk);
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//获取行列索引
//第一种方式
//int row = e.RowIndex + 1;
//int col = e.ColumnIndex + 1;
//第二种方式
int col = dataGridView1.CurrentCell.ColumnIndex + 1;
int row = dataGridView1.CurrentCell.RowIndex + 1;
//第三种方式
//int col = dataGridView1.CurrentCellAddress.X + 1;
//int row = dataGridView1.CurrentCellAddress.Y + 1;
//第四种方式
// int row = dataGridView1.CurrentRow.Index+1;
// int col = dataGridView1.CurrentCell.ColumnIndex + 1;
//获取单元格内容
//第一种方式:
// string str = dataGridView1.CurrentCell.Value.ToString();
//第二种方式:
string str = dataGridView1.Rows[row-1].Cells[col-1].Value.ToString();
MessageBox.Show("点击了第"+row.ToString()+"行,"+col.ToString()+"列"+"\n内容是:"+str);
// 获取指定行列内容
string str2 = dataGridView1.Rows[3 - 1].Cells[4 - 1].Value.ToString();//获取3行4列内容.
MessageBox.Show(str2);
}
下面是运行效果: