如何判断DataGridView是否被全选?(WinForm)

解决方案 »

  1.   

    bool CheckSelectedStated(DataGridView dgv)
    {
       foreach(DataRow row in dgv.Rows)
       {
          if(!row.Selected)
          {
             return false;
          }
       }
       return false;
    }
      

  2.   

    if (this.dataGridView1.SelectedRows.Count<this.dataGridView1.Rows.Count)
                {
                    this.dataGridView1.SelectAll();
                }
      

  3.   

    画个全选滴框框出来~
        class DGVColumnHeader : DataGridViewColumnHeaderCell
        {
            private Rectangle CheckBoxRegion;
            private bool checkAll = false;        protected override void Paint(Graphics graphics,
                Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
                DataGridViewElementStates dataGridViewElementState,
                object value, object formattedValue, string errorText,
                DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
                DataGridViewPaintParts paintParts)
            {            base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value,
                    formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);            graphics.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds);            CheckBoxRegion = new Rectangle(
                    cellBounds.Location.X + 3,
                    cellBounds.Location.Y + 2,
                    25, cellBounds.Size.Height - 4);
                if (this.checkAll)
                    ControlPaint.DrawCheckBox(graphics, CheckBoxRegion, ButtonState.Checked);
                else
                    ControlPaint.DrawCheckBox(graphics, CheckBoxRegion, ButtonState.Normal);            Rectangle normalRegion =
                    new Rectangle(
                    cellBounds.Location.X + 1 + 25,
                    cellBounds.Location.Y,
                    cellBounds.Size.Width - 26,
                    cellBounds.Size.Height);            graphics.DrawString(value.ToString(), cellStyle.Font, new SolidBrush(cellStyle.ForeColor), normalRegion);
            }        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
            {
                //Convert the CheckBoxRegion 
                Rectangle rec = new Rectangle(new Point(0, 0), this.CheckBoxRegion.Size);
                this.checkAll = !this.checkAll;
                if (rec.Contains(e.Location))
                {
                    this.DataGridView.Invalidate();
                }
                base.OnMouseClick(e);
            }        public bool CheckAll
            {
                get { return this.checkAll; }
                set { this.checkAll = value; }
            }
        }
    全选滴事件private void dgv_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                DataGridView dgv = (DataGridView)sender;
                if (e.ColumnIndex == 0)
                {
                    for (int i = 0; i < dgv.Rows.Count; i++)
                    {
                        dgv.Rows[i].Cells[0].Value = dgvColumnHeader.CheckAll;
                    }
                }
            }