DataGridViewCheckBoxColumn的勾选框勾上了,Value却是false

 

   private  void dgvWebSiteOrder_CellClick( object sender, DataGridViewCellEventArgs e)
        {

          //此事件里面不能获取
           if (row.Cells[ this.colSelect.Name].Value != null && row.Cells[ this.colSelect.Name].Value.ToString() == " 1 "
        }

  private void dgvLinkOrderList_CellContentClick(object sender, DataGridViewCellEventArgs e)
  {
         //此事件里面不能获取
         if (e.ColumnIndex < 0 || e.RowIndex < 0)
            {
                return;
            }
    
  }

//此事件里面可以获取
  private  void dgvWebSiteOrder_CellValueChanged( object sender, DataGridViewCellEventArgs e)
        {
            List<DataGridViewRow> selectRows =  new List<DataGridViewRow>();
             // 点击的是第一列。
             if (e.ColumnIndex ==  0)
            {
                 foreach (DataGridViewRow row  in  this.dgvWebSiteOrder.Rows)
                {
                     if (row.Cells[ this.colSelect.Name].Value !=  null && row.Cells[ this.colSelect.Name].Value.ToString() ==  " 1 ")
                    {
                        selectRows.Add(row);
                    }
                }
            }
            DgvSelectRows = selectRows;
        }

 

本文转载http://www.cnblogs.com/gossip/archive/2008/12/02/1346047.htmldatagridview的checkbox列,当修改checkbox状态时实时获得其状态值

   不知道大家有没有这样的经验,当点击或者取消datagridview的checkbox列时,比较难获得其状态是选中还是未选中,进而不好进行其它操作,下面就列出它的解决办法:

主要用到了datagridview的CurrentCellDirtyStateChanged和CellValueChanged两个事件

CurrentCellDirtyStateChanged事件是提交对checkbox状态的修改

CellValueChanged事件是当状态提交后,也就是单元格值改变后做一些其它的操作,这里是将checkbox列的true或false状态作为tooptiptext属性设置到同一行的button列

CurrentCellDirtyStateChanged事件代码 :


private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (this.dataGridView1.IsCurrentCellDirty) //有未提交的更//
{
               
this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}

CellValueChanged事件代码 :


private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
           
if (this.dataGridView1.Columns[e.ColumnIndex].Name.Equals("gender"))
            {
                DataGridViewButtonCell dgvButtonCell
= this.dataGridView1.Rows[e.RowIndex].Cells["btn"] as DataGridViewButtonCell;//获得button列单元格
                DataGridViewCheckBoxCell dgvCheckBoxCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;//获得checkbox列单元格
                dgvButtonCell.ToolTipText = dgvCheckBoxCell.Value.ToString();//赋值
            }
        }

你可能感兴趣的:(datagridview)