如何通过点击标题的checkbox获取父控件DataGridview的引用?winform的。
        /// <summary>
        /// 全选或取消全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void headCheckBoxClicked(object sender, DatagridviewCheckboxHeaderEventArgs e)
        {
            DataGridView dgv = ??
            try
            {
                foreach (DataGridViewRow row in (IEnumerable)dgv.Rows)
                {
                    row.Cells["IsCheck"].Value = e.CheckedState;
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

解决方案 »

  1.   

    直接用 this.dataGridView1 引用就可以了吧。
      

  2.   

    我的窗体有几个dataGridView,都有这个事件,我希望直接从事件中获取dataGridView,共用一个事件函数,而不用为每个dataGridView写不同的事件函数。
      

  3.   

    private DataGridView DataGridView1 = new DataGridView();
            private CheckBox CheckBox1 = new CheckBox();         public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                CheckBox1.CheckedChanged += CheckBox1_CheckedChanged;
                DataGridView1.CellPainting += DataGridView1_CellPainting;
                this.DataGridView1.AllowUserToResizeRows = false;
                this.DataGridView1.AllowUserToResizeColumns = false;
                this.DataGridView1.Dock = DockStyle.Fill;            this.DataGridView1.Columns.Add(new DataGridViewCheckBoxColumn());
                this.DataGridView1.Columns.Add("Column2", "Column2");            for (int i = 1; i <= 3; i++)
                {
                    this.DataGridView1.Rows.Add(0, "Row" + i.ToString() + " Column2");
                }            this.CheckBox1.Visible = false;
                this.CheckBox1.Text = "CheckBox";            this.Controls.Add(DataGridView1);
                this.Controls.Add(CheckBox1); 
            }        private void CheckBox1_CheckedChanged(object send, System.EventArgs e)
            {
                for (int i = 0; i <= this.DataGridView1.RowCount - 1; i++)
                {
                    this.DataGridView1.Rows.SharedRow(i).SetValues(CheckBox1.Checked);
                }
            }        private void DataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
            {
                if (e.RowIndex == -1 & e.ColumnIndex == 0)
                {
                    Point p = this.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location;
                    p.Offset(this.DataGridView1.Left, this.DataGridView1.Top);
                    this.CheckBox1.Location = p;
                    this.CheckBox1.Size = this.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Size;
                    this.CheckBox1.Visible = true;
                    this.CheckBox1.BringToFront();
                }
            } 
      

  4.   

    如果你的 DatagridviewCheckboxHeader 是继承自 DataGridViewColumnHeaderCell 的,那么
    DataGridView dgv = (sender as DataGridViewColumnHeaderCell).DataGridView;