码迷,mamicode.com
首页 > Windows程序 > 详细

15.3获取DataGridView的当前单元格的索引和内容

时间:2016-04-07 20:24:03      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

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

namespace _15._3DataGridView单元格
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“csharpzxwDataSet.mytable001”中。您可以根据需要移动或删除它。
            this.mytable001TableAdapter.Fill(this.csharpzxwDataSet.mytable001);

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //获取行列索引
            //第一种方式
            //int row = e.RowIndex + 1;
            //int col = e.ColumnIndex + 1;

            //第二种方式
            //int row = dataGridView1.CurrentCell.RowIndex + 1;
            //int col = dataGridView1.CurrentCell.ColumnIndex + 1;

            //第三种方式
            int col = dataGridView1.CurrentCellAddress.X+1;
            //int row = dataGridView1.CurrentCellAddress.Y+1;

            //第四种方式行的引用
            int row = dataGridView1.CurrentRow.Index+1;

            //获取单元格的内容
            //1种方法
            //string cell = dataGridView1.Rows[row-1].Cells[col-1].Value.ToString();
            //2种方法
            string cell = dataGridView1.CurrentCell.Value.ToString();



            MessageBox.Show("您点击的是:第"+row.ToString()+"行,第"+col.ToString()+"列,内容为:"+cell);
        }
    }
}

 

15.3获取DataGridView的当前单元格的索引和内容

标签:

原文地址:http://www.cnblogs.com/zqyo2000z/p/5365024.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!