C# Winform中DataGridView的DataGridViewCheckBoxColumn使用方法

C# Winform中DataGridView的DataGridViewCheckBoxColumn使用方法

分类: WinForm 2627人阅读 评论(2) 收藏 举报

下面介绍Winform中DataGridView的DataGridViewCheckBoxColumn使用方法:

DataGridViewCheckBoxColumn CheckBox是否选中

  在判断DataGridView中CheckBox选中列的时候,用 DataGridViewRow.Cells[0].FormattedValue.ToString()=="True"语句时存在问题,当我们直接点击CheckBox时,结果显示未选中,但是如果我们在点击其他单元格时,结果显示选中。而用 DataGridViewRow.Cells[0].EditedFormattedValue.ToString()=="True"语句时不管怎么样是选中的状态。

为什么会有这种结果?

  原因:就是FormattedValue是操作提交后的结果,而EditedFormattedValue是当前的结果,不管结果是否已经提交。

     所以用DataGridViewRow.Cells[0].EditedFormattedValue.ToString()=="True"判断选中比较合适

[c-sharp] view plain copy print ?
  1. if (dgvDownloadList.Rows.Count > 0)  
  2. {  
  3.     for (int i = 0; i < dgvDownloadList.Rows.Count; i++)  
  4.     {  
  5.         string _selectValue = dgvDownloadList.Rows[i].Cells["Column1"].EditedFormattedValue.ToString();  
  6.         if (_selectValue == "True")  
  7.            //如果CheckBox已选中,则在此处继续编写代码   
  8.      }  
  9. }   

if (dgvDownloadList.Rows.Count > 0) { for (int i = 0; i < dgvDownloadList.Rows.Count; i++) { string _selectValue = dgvDownloadList.Rows[i].Cells["Column1"].EditedFormattedValue.ToString(); if (_selectValue == "True") //如果CheckBox已选中,则在此处继续编写代码 } }

DataGridViewCheckBoxColumn 设置CheckBox默认选中

   ((DataGridViewCheckBoxCell)dgvDownloadList.Rows[i].Cells["Column1"]).Value = true;

DataGridViewCheckBoxColumn 第一时间获取CheckBox的选中状态

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

  CommitEdit :将当前单元格中的更改提交到数据缓存,但不结束编辑模式 

[c-sharp] view plain copy print ?
  1. dgvDownloadList.CurrentCellDirtyStateChanged += new EventHandler(dgvDownloadList_CurrentCellDirtyStateChanged);  
  2.  dgvDownloadList.CellValueChanged += new DataGridViewCellEventHandler(dgvDownloadList_CellValueChanged);  
  3.     
  4. void dgvDownloadList_CurrentCellDirtyStateChanged(object sender, EventArgs e)  
  5. {  
  6.      if (dgvDownloadList.IsCurrentCellDirty)  
  7.      {  
  8.          dgvDownloadList.CommitEdit(DataGridViewDataErrorContexts.Commit);  
  9.      }              
  10. }  
  11.     
  12. void dgvDownloadList_CellValueChanged(object sender, DataGridViewCellEventArgs e)  
  13. {  
  14.      if (dgvDownloadList.Rows.Count > 0)  
  15.      {  
  16.          for (int i = 0; i < dgvDownloadList.Rows.Count; i++)  
  17.          {  
  18.              string _selectValue = dgvDownloadList.Rows[i].Cells["Column1"].EditedFormattedValue.ToString();  
  19.              if (_selectValue == "True")  
  20.                  //如果CheckBox已选中,则在此处继续编写代码   
  21.          }  
  22.       }  
  23. }  

dgvDownloadList.CurrentCellDirtyStateChanged += new EventHandler(dgvDownloadList_CurrentCellDirtyStateChanged); dgvDownloadList.CellValueChanged += new DataGridViewCellEventHandler(dgvDownloadList_CellValueChanged); void dgvDownloadList_CurrentCellDirtyStateChanged(object sender, EventArgs e) { if (dgvDownloadList.IsCurrentCellDirty) { dgvDownloadList.CommitEdit(DataGridViewDataErrorContexts.Commit); } } void dgvDownloadList_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (dgvDownloadList.Rows.Count > 0) { for (int i = 0; i < dgvDownloadList.Rows.Count; i++) { string _selectValue = dgvDownloadList.Rows[i].Cells["Column1"].EditedFormattedValue.ToString(); if (_selectValue == "True") //如果CheckBox已选中,则在此处继续编写代码 } } }

转载于:https://www.cnblogs.com/baishiying/archive/2012/09/13/2683242.html

下面介绍Winform中DataGridView的DataGridViewCheckBoxColumn使用方法:

DataGridViewCheckBoxColumn CheckBox是否选中

  在判断DataGridView中CheckBox选中列的时候,用 DataGridViewRow.Cells[0].FormattedValue.ToString()=="True"语句时存在问题,当我们直接点击CheckBox时,结果显示未选中,但是如果我们在点击其他单元格时,结果显示选中。而用 DataGridViewRow.Cells[0].EditedFormattedValue.ToString()=="True"语句时不管怎么样是选中的状态。

为什么会有这种结果?

  原因:就是FormattedValue是操作提交后的结果,而EditedFormattedValue是当前的结果,不管结果是否已经提交。

     所以用DataGridViewRow.Cells[0].EditedFormattedValue.ToString()=="True"判断选中比较合适

[c-sharp] view plain copy print ?
  1. if (dgvDownloadList.Rows.Count > 0)  
  2. {  
  3.     for (int i = 0; i < dgvDownloadList.Rows.Count; i++)  
  4.     {  
  5.         string _selectValue = dgvDownloadList.Rows[i].Cells["Column1"].EditedFormattedValue.ToString();  
  6.         if (_selectValue == "True")  
  7.            //如果CheckBox已选中,则在此处继续编写代码   
  8.      }  
  9. }   

if (dgvDownloadList.Rows.Count > 0) { for (int i = 0; i < dgvDownloadList.Rows.Count; i++) { string _selectValue = dgvDownloadList.Rows[i].Cells["Column1"].EditedFormattedValue.ToString(); if (_selectValue == "True") //如果CheckBox已选中,则在此处继续编写代码 } }

DataGridViewCheckBoxColumn 设置CheckBox默认选中

   ((DataGridViewCheckBoxCell)dgvDownloadList.Rows[i].Cells["Column1"]).Value = true;

DataGridViewCheckBoxColumn 第一时间获取CheckBox的选中状态

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

  CommitEdit :将当前单元格中的更改提交到数据缓存,但不结束编辑模式 

[c-sharp] view plain copy print ?
  1. dgvDownloadList.CurrentCellDirtyStateChanged += new EventHandler(dgvDownloadList_CurrentCellDirtyStateChanged);  
  2.  dgvDownloadList.CellValueChanged += new DataGridViewCellEventHandler(dgvDownloadList_CellValueChanged);  
  3.     
  4. void dgvDownloadList_CurrentCellDirtyStateChanged(object sender, EventArgs e)  
  5. {  
  6.      if (dgvDownloadList.IsCurrentCellDirty)  
  7.      {  
  8.          dgvDownloadList.CommitEdit(DataGridViewDataErrorContexts.Commit);  
  9.      }              
  10. }  
  11.     
  12. void dgvDownloadList_CellValueChanged(object sender, DataGridViewCellEventArgs e)  
  13. {  
  14.      if (dgvDownloadList.Rows.Count > 0)  
  15.      {  
  16.          for (int i = 0; i < dgvDownloadList.Rows.Count; i++)  
  17.          {  
  18.              string _selectValue = dgvDownloadList.Rows[i].Cells["Column1"].EditedFormattedValue.ToString();  
  19.              if (_selectValue == "True")  
  20.                  //如果CheckBox已选中,则在此处继续编写代码   
  21.          }  
  22.       }  
  23. }  

dgvDownloadList.CurrentCellDirtyStateChanged += new EventHandler(dgvDownloadList_CurrentCellDirtyStateChanged); dgvDownloadList.CellValueChanged += new DataGridViewCellEventHandler(dgvDownloadList_CellValueChanged); void dgvDownloadList_CurrentCellDirtyStateChanged(object sender, EventArgs e) { if (dgvDownloadList.IsCurrentCellDirty) { dgvDownloadList.CommitEdit(DataGridViewDataErrorContexts.Commit); } } void dgvDownloadList_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (dgvDownloadList.Rows.Count > 0) { for (int i = 0; i < dgvDownloadList.Rows.Count; i++) { string _selectValue = dgvDownloadList.Rows[i].Cells["Column1"].EditedFormattedValue.ToString(); if (_selectValue == "True") //如果CheckBox已选中,则在此处继续编写代码 } } }

猜你喜欢

转载自blog.csdn.net/weixin_34050427/article/details/93440055