Adding a checkbox and common processing in DataGridView
Article 1Reprint: http://blog.csdn.net/pinkey1987/article/details/5267934
There are two main ways to add a CheckBox control in DataGridView
1. Add columns of type System.Windows.Forms.DataGridViewCheckBoxColumn by columns in DataGridView. And you can set the property information related to the column.
2. Add the appropriate code directly in the program code
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});
You can also use THIS.DGVRET.COLUMNS.ADD (COLUMN1) For more flexible column additions.
After the DataGridView binds the data source, You can determine the properties and detailed column names of the data sources that affect the checkbox display by setting the datapropertyname of the Datagridviewcheckboxcolumn (how you can influence the TrueValue, Falsevalue and Indeterminatevalue to set). You can also have data sources that are not associated with the added checkbox and appear as different columns in DataGridView.
The common processing method of checkbox in DataGridView.
1. How to set the value of the checkbox.
This.dgvret.rows[i]. CELLS[J]. Value = false;
Set the value of the checkbox directly in the DataGridView (i+1) row (j+1) column. If value is True,checkbox is ticked, if value is false,checkbox not ticked.
2. CheckBox whether to check the event handling method.
It is important to note that adding Datagridviewcheckboxcolumn,checkbox in DataGridView is the entire cell box. In C #, by default, the checkbox does not have a necessary connection between the click and the tick. It is possible that when you click the Cell box, the checkbox's value (whether it is ticked) does not change (point is in the space in the cell).
In this case, I generally control the value of the checkbox through the program. Does not rely on visual Studio 2005 auto-completion. The specific processing steps are as follows:
1. Set the ReadOnly property of the Datagridviewcheckboxcolumn to true.
2. Add the DataGridView Cellclick method.
private void Dgvret_cellclick (object sender, DataGridViewCellEventArgs e)
{
if (This.dgvRet.CurrentCell.ColumnIndex = = 0)
{
Gets the cell of the checkbox in the DataGridView
Datagridviewcheckboxcell Dgvcheck = (Datagridviewcheckboxcell) (This.dgvret.rows[this.dgvret.currentcell.rowindex] . Cells[0]);
Get information about the selected column
Double Dadd = Double. Parse (This.dgvret.rows[this.dgvret.currentcell.rowindex]. CELLS[5]. Value.tostring ());
...
The cell's value is processed according to the click. Both Editedformattedvalue and value can be
If clicked, the checkbox is not ticked
if (Convert.toboolean (dgvcheck.editedformattedvalue) = = False)
{
This.dtotal + = Dadd;
...
Check whether the checkbox is controlled by the program
Dgvcheck.value = true;
}
If clicked, the checkbox has been ticked
Else
{
This.dtotal-= Dadd;
...
Check whether the checkbox is controlled by the program
Dgvcheck.value = false;
}
This.txtTotal.Text = This.dTotal.ToString ();
}
}
Article 2 How to use the checkbox in DataGridView in C # winfrom (GO)
Http://blog.sina.com.cn/s/blog_4e51b5530100j5yz.html
Method One:
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)//Find the selected data row
{
Checkcell.value = false;
}
Else
Continue
}
}
}
Get the selected data
int count = Convert.ToInt32 (Dgv_zy. Rows.Count.ToString ());
for (int i = 0; i < count; i++)
{
If DataGridView is editable, the data is submitted, otherwise the line in the edit state cannot be taken to the
Dgv_zy. EndEdit ();
Datagridviewcheckboxcell Checkcell = (Datagridviewcheckboxcell) dgv_zy. Rows[i]. cells["Cb_check"];
Boolean flag = Convert.toboolean (Checkcell.value);
if (flag = = true)//Find the selected data row
{
Getting data items from DATAGRIDVIEW
String z_zcode = Dgv_zy. Rows[i]. Cells[0]. Value.tostring (). Trim ();
}
}
Method Two:
If you need to embed a checkbox column (Datagridviewcheckboxcell) in the WinForm Data Control DataGridView,
In the execution of the program it is possible to capture the change of its state by the same event as the SelectedIndexChanged event of the Pure CheckBox control
I think the better way is to use the DataGridView control's CellContentClick event for example:
If the embedded Datagridviewcheckboxcell column is in the first column, the status is determined and the processing event can be added as:
private void Datagridview1_cellcontentclick (object sender, DataGridViewCellEventArgs e)
{
if (E.columnindex = = 0 && E. RowIndex! =-1)
{
Gets the value of the control
MessageBox.Show (This.datagridview1.rows[e.rowindex]. Cells[0]. Editedformattedvalue.tostring ());
Or you can do other event handlers
}
}
It is important to note that performing this event is a CellContentClick event that requires masking other DataGridView cells, that is, except for the Datagridviewcheckboxcell column
The readonly=true of all columns except;
When getting the checkbox column in DataGridView, be sure to use the Editedformattedvalue property, which gets the value of the edit values and
FormattedValue often returns an edit of a previous value, while repeated clicks tend to cause an error (it is not possible to determine whether it is a pre-edit or post-edit value: Main
The reason is the focus problem, need to move the focus first so that DataGridView get the changed data in the area to get him there is no problem, so later to use to get the data before moving out
DataGridView in the spotlight!!! ), so be sure to use Editedformattedvalue to get the property value