c# datagridview选中当前单元格及单元格单击事件

public partial class Form2 : Form
    {
        OracleDataAdapter oda;
        DataSet ds1;
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToAddRows = true;
            //如果选中DATAGRIDVIEW某行其中某个单元格(列)是空值,进行相应的特殊处理
            //data source,user id,password不区分大小写
            OracleConnection con = new OracleConnection("data Source=orcl;user id=scott;password=system");
            OracleCommand comm = new OracleCommand();
            comm.Connection = con;
            comm.CommandText = "select deptno,dname,loc from dept";
            comm.CommandType = CommandType.Text;
            da= new OracleDataAdapter(comm);
            ds1 = new DataSet();
            oda.Fill(ds1);
            dataGridView1.DataSource = ds1.Tables[0];

        }


        //datagridview_cellcontentclick单击单元格内容发生此事件
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
          
        }

        //cellclick单元格任何部分发生此事件
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //currentcell当前活动单元格 columnindex 当前活动单元格的索引

            // this.textBox1.Text = dataGridView1.SelectedCells[dataGridView1.CurrentCell.ColumnIndex]
            //查看此单元格的值currentcell.value
            if (dataGridView1.CurrentCell.Value.ToString()=="")
            {
                MessageBox.Show("你选中的单元格无值");
            }
            this.textBox1.Text = dataGridView1.CurrentCell.Value.ToString() + dataGridView1.CurrentCell.ColumnIndex.ToString();
            //selectedcells.tostring返回类型的名字信息,而不是单元格的内容,即显示datagridviewselectedcellcollection
           // this.textBox1.Text = dataGridView1.SelectedCells.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //利用oraclecommandbuilder封装oracledataadapter,然后用update来更新数据库表
            OracleCommandBuilder cb = new OracleCommandBuilder(oda);
            oda.Update(ds1.Tables[0]);
        }

       
    }

请使用浏览器的分享功能分享到微信等