winform dgv右键选择

 private void dgv_dataBind()
        {
            //构建表
            DataTable dtStat = new DataTable();

            dtStat.Columns.Add("DepartmentId"); 
            dtStat.Columns.Add("YDMB");    

            DataRow drStat = dtStat.NewRow();
            drStat[0] = "1";
            drStat[1] = "2222";
            dtStat.Rows.Add(drStat);
            drStat = dtStat.NewRow();
            drStat[0] = "2";
            drStat[1] = "ffff";
            dtStat.Rows.Add(drStat);
            drStat = dtStat.NewRow();
            drStat[0] = "3";
            drStat[1] = "fffggg";
            dtStat.Rows.Add(drStat);
            dataGridView1.DataSource = dtStat;

            dataGridView1.Columns[1].Visible = false;
        }

        private void delToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //选中的行中某一个单元格的值
            string i = dataGridView1.SelectedCells[0].Value.ToString();
            MessageBox.Show(i);
            ////MessageBox.Show(dataGridView1.Rows[i].Cells[1].ToString());
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            //右键选择dgv的行
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[e.RowIndex].Selected = true;
                    //string i = dataGridView1.SelectedCells[0].Value.ToString();
                    //MessageBox.Show(i);
                    //dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    //在鼠标位置显示右键菜单
                    //contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }

posted @ 2008-07-31 13:33  jenner  阅读(257)  评论(0编辑  收藏  举报