DataGridView 中添加CheckBox和常用處理方式
文章1 轉載:http://blog.csdn.net/pinkey1987/article/details/5267934
DataGridView中添加CheckBox控件主要采用兩種方法
1. 通過在DataGridView的Columns中添加System.Windows.Forms.DataGridViewCheckBoxColumn類型的列。並可以設置該列相關的屬性信息。
2. 在程序代碼中直接添加相應的代碼
System.Windows.Forms.DataGridViewCheckBoxColumn Column1;
Column1 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
Column1.HeaderText = "Column1";
Column1.Name = "Column1";
Column1.ReadOnly = false;
...
this.dgvRet.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {Column1});
也可以采用this.dgvRet.Columns.Add(Column1);進行更靈活的列添加。
在DataGridView綁定數據源后,可以通過設置DataGridViewCheckBoxColumn的DataPropertyName來確定影響CheckBox顯示的數據源的屬性和詳細列名(具體如何影響可以由TrueValue、FalseValue和IndeterminateValue來設置)。也可以讓數據源與添加的CheckBox無關聯,在DataGridView中顯示為不同的列。
DataGridView 中CheckBox的常用處理方法。
1. 如何設置CheckBox的值。
this.dgvRet.Rows[i].Cells[j].Value = false;
直接設置在DataGridView第(i+1)行第(j+1)列中CheckBox的Value的值。若Value的值為true,CheckBox 被勾上,若Value的值為false,CheckBox 不勾上。
2. CheckBox是否勾上的事件處理方法。
需要注意的是:在DataGridView中添加DataGridViewCheckBoxColumn,CheckBox是占有整個Cell方框的。在c#中默認情況下,CheckBox是否被單擊和是否勾上沒有必然的聯系。可能你在點擊Cell方框后,CheckBox的Value(是否勾上)不會發生變化(點在Cell中空白處)。
這種情況,我一般將CheckBox的Value值通過程序進行控制。不依賴於Visual Studio 2005自動完成。具體處理步驟如下:
1. 將DataGridViewCheckBoxColumn的ReadOnly屬性設置為true。
2. 添加DataGridView的CellClick方法。
private void dgvRet_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (this.dgvRet.CurrentCell.ColumnIndex == 0)
{
//獲取DataGridView中CheckBox的Cell
DataGridViewCheckBoxCell dgvCheck = (DataGridViewCheckBoxCell)(this.dgvRet.Rows[this.dgvRet.CurrentCell.RowIndex].Cells[0]);
//獲取被選中列的相關信息
Double dAdd = double.Parse(this.dgvRet.Rows[this.dgvRet.CurrentCell.RowIndex].Cells[5].Value.ToString());
...
//根據單擊時,Cell的值進行處理。EditedFormattedValue和Value均可以
//若單擊時,CheckBox沒有被勾上
if (Convert.ToBoolean(dgvCheck.EditedFormattedValue) == false)
{
this.dTotal += dAdd;
...
//通過程序完成CheckBox是否勾上的控制
dgvCheck.Value = true;
}
//若單擊時,CheckBox已經被勾上
else
{
this.dTotal -= dAdd;
...
//通過程序完成CheckBox是否勾上的控制
dgvCheck.Value = false;
}
this.txtTotal.Text = this.dTotal.ToString();
}
}
文章2 C# winfrom 中datagridview中checkbox的使用方法(轉)
http://blog.sina.com.cn/s/blog_4e51b5530100j5yz.html
方法一:
private void dgv_zy_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int count = Convert.ToInt16(dgv_zy.Rows.Count.ToString());
for (int i = 0; i < count; i++)
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dgv_zy.Rows[i].Cells["cb_check"];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == true) //查找被選擇的數據行
{
checkCell.Value = false;
}
else
continue;
}
}
}
獲取選擇的數據
int count = Convert.ToInt32(dgv_zy.Rows.Count.ToString());
for (int i = 0; i < count; i++)
{
//如果DataGridView是可編輯的,將數據提交,否則處於編輯狀態的行無法取到
dgv_zy.EndEdit();
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dgv_zy.Rows[i].Cells["cb_check"];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == true) //查找被選擇的數據行
{
//從 DATAGRIDVIEW 中獲取數據項
string z_zcode = dgv_zy.Rows[i].Cells[0].Value.ToString().Trim();
}
}
方法二:
如果需要在winform 的數據控件datagridview 中嵌入checkbox列 ( DataGridViewCheckBoxCell ),
在程序的執行中有可能需要像純粹的checkbox控件的selectedindexchanged事件一樣的事件來捕捉其狀態的改變
我覺得比較好的方式是用datagridview 控件的cellcontentclick事件 例如:
如果嵌入的 DataGridViewCheckBoxCell 列在第一列,判斷狀態並添加處理事件可以為:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && e .RowIndex != -1)
{
//獲取控件的值
MessageBox.Show(this.dataGridView1.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString());
//或者可以做其他事件處理程序
}
}
需要注意的是執行此事件是需要屏蔽其他datagridview單元格的cellcontentclick事件 ,即讓除了 DataGridViewCheckBoxCell 列
之外的所有列的ReadOnly=True;
在獲取datagridview中checkbox列的值得時候 一定要用 EditedFormattedValue屬性,此屬性獲取的是編輯以后數值 而value 和
FormattedValue返回的往往是編輯以前的數值,而其重復單擊的時候往往會出現錯誤(無法確定是編輯前還是編輯后的數值: 主要
原因是焦點問題,需要先移動焦點使datagridview獲取更改后的數據在區獲取他就沒有問題了,所以以后用去獲取數據前先要移出
datagridview中的焦點!!!),所以一定要用EditedFormattedValue來獲取屬性值