The following describes a DataGridView Winform DataGridViewCheckBoxColumn in use:
DataGridViewCheckBoxColumn the CheckBox is selected or
In determining the DataGridView CheckBox selected column when [0] exist with DataGridViewRow.Cells .FormattedValue.ToString () == "True" when the statement is a problem, when we click on CheckBox, the results show is not selected, but if we click when other cells, the results show selected. And with DataGridViewRow.Cells [0] .EditedFormattedValue.ToString () == "True" when the statement is selected by the state anyway.
Why is there such a result?
The reason: that is the result of the operation FormattedValue submitted, and EditedFormattedValue is the current result, regardless of whether the results have been submitted.
Therefore, the use DataGridViewRow.Cells [0] .EditedFormattedValue.ToString () == "True" Analyzing select suitable
- 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")
- // If the CheckBox is selected, then write the code in here to continue
- }
- }
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") // If the CheckBox is checked, continue to write code here}}
DataGridViewCheckBoxColumn set the CheckBox checked by default
((DataGridViewCheckBoxCell)dgvDownloadList.Rows[i].Cells["Column1"]).Value = true;
DataGridViewCheckBoxColumn the first time to get checked in CheckBox
When you click a checkbox or cancel datagridview column, the more difficult to obtain its status is checked or unchecked, then good for other operations, the following list of its solution:
CommitEdit: will be submitted to the current cell changes to the data cache without ending edit mode
- 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")
- // If the CheckBox is selected, then write the code in here to continue
- }
- }
- }
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已选中,则在此处继续编写代码 } } }
Reproduced in: https: //www.cnblogs.com/baishiying/archive/2012/09/13/2683242.html
The following describes a DataGridView Winform DataGridViewCheckBoxColumn in use:
DataGridViewCheckBoxColumn the CheckBox is selected or
In determining the DataGridView CheckBox selected column when [0] exist with DataGridViewRow.Cells .FormattedValue.ToString () == "True" when the statement is a problem, when we click on CheckBox, the results show is not selected, but if we click when other cells, the results show selected. And with DataGridViewRow.Cells [0] .EditedFormattedValue.ToString () == "True" when the statement is selected by the state anyway.
Why is there such a result?
The reason: that is the result of the operation FormattedValue submitted, and EditedFormattedValue is the current result, regardless of whether the results have been submitted.
Therefore, the use DataGridViewRow.Cells [0] .EditedFormattedValue.ToString () == "True" Analyzing select suitable
- 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")
- // If the CheckBox is selected, then write the code in here to continue
- }
- }
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") // If the CheckBox is checked, continue to write code here}}
DataGridViewCheckBoxColumn set the CheckBox checked by default
((DataGridViewCheckBoxCell)dgvDownloadList.Rows[i].Cells["Column1"]).Value = true;
DataGridViewCheckBoxColumn the first time to get checked in CheckBox
When you click a checkbox or cancel datagridview column, the more difficult to obtain its status is checked or unchecked, then good for other operations, the following list of its solution:
CommitEdit: will be submitted to the current cell changes to the data cache without ending edit mode
- 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")
- // If the CheckBox is selected, then write the code in here to continue
- }
- }
- }
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已选中,则在此处继续编写代码 } } }