Let's introduce Winform in DataGridView Of DataGridViewCheckBoxColumn Usage method :

DataGridViewCheckBoxColumn CheckBox Is it checked?

In judging DataGridView in CheckBox When the column is selected , use DataGridViewRow.Cells[0].FormattedValue.ToString()=="True" There is a problem with the statement , When we click directly CheckBox when , The result shows that... Is not selected , But if we click on other cells , The results show that . While using DataGridViewRow.Cells[0].EditedFormattedValue.ToString()=="True" Statement is the selected state anyway .

Why did this happen ?

reason : Namely FormattedValue It's the result of operation submission , and EditedFormattedValue It's the current result , Whether or not the results have been submitted .

So use DataGridViewRow.Cells[0].EditedFormattedValue.ToString()=="True" It's better to choose

if (dgvDownloadList.Rows.Count > )
{
for (int i = ; i < dgvDownloadList.Rows.Count; i++)
{
string _selectValue = dgvDownloadList.Rows[i].Cells["Column1"].EditedFormattedValue.ToString();
if (_selectValue == "True")
// If CheckBox Selected , Continue to write code here
}
}

DataGridViewCheckBoxColumn  Set up CheckBox Selected by default

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

DataGridViewCheckBoxColumn  Get... The first time CheckBox Selected state of

When you click or cancel datagridview Of checkbox Column time , It's hard to get whether it's selected or not , And then it's not easy to do other operations , Here are the solutions :

CommitEdit : Commit changes in the current cell to the data cache , But don't end 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 > )
{
for (int i = ; i < dgvDownloadList.Rows.Count; i++)
{
string _selectValue = dgvDownloadList.Rows[i].Cells["Column1"].EditedFormattedValue.ToString();
if (_selectValue == "True")
// If CheckBox Selected , Continue to write code here
}
}
}

Reprint http://www.cnblogs.com/xucan/archive/2010/12/05/1897025.html

C# Winform in DataGridView Of DataGridViewCheckBoxColumn More articles on how to use it

  1. C# Winform in DataGridView Of DataGridViewCheckBoxColumn CheckBox Select judgment

    1.DataGridViewCheckBoxColumn CheckBox Is it checked? In judging DataGridView in CheckBox When the column is selected , use DataGridViewRow.Cells[0].F ...

  2. winform in DataGridView Use DataGridViewCheckBoxColumn Realization RadioBox Radio function

    private void dgvMaterial_CellContentClick(object sender, DataGridViewCellEventArgs e) { ; i < dgv ...

  3. 【 Connected to a 】winform in dataGridView Height and width adaptively fill in the height and width of the data , namely dataGridView Adaptive size based on data

    Last one :winform in dataGridView Height adaptively fills the height of the data winform in dataGridView Height adaptively fills the height of the data , Namely dataGridView It doesn't create scrollbars of its own , My own height ...

  4. Reprint :WinForm There are three ways to play sound in

    Reprint :WinForm There are three ways to play sound in Peter Jackson's King Kong winForm The play This is a reprinted article . The source of the original text is :http://blog.csdn.net/jijunwu/article/details/4753 ...

  5. winform in dataGridView Cells set new values based on values , Completely solve the problem of data type conversion after binding

    // winform in dataGridView Cell after data binding , Data type change is difficult , It can only be achieved by detour . Sometimes it's necessary to transform numbers into different text descriptions , Will appear int32 To string Exception to type conversion , With the help of CellFo ...

  6. winform in dataGridView Interlaced with different background colors , Different colors are displayed on the mouse , Go back to the original color after you leave

    winform in dataGridView Interlaced with different background colors , Different colors are displayed on the mouse , Go back to the original color after you leave Set the color of the odd number of lines first , This has its own property AlternatingRowsDefaultCellStyle ...

  7. winform in dataGridView Height adaptively fills the height of the data

    // winform in dataGridView Height adaptively fills the height of the data , Namely dataGridView It doesn't create scrollbars of its own , My height changes according to the amount of data . stay load When , After data binding , Add the following code : ...

  8. C# winform in Window scaling adaptive method ( Different computers / Different resolutions )

    C# winform in Window scaling adaptive method ( Different computers / Different resolutions )   Window zooming is a problem that has been bothering me for a long time , To solve this problem , I found a lot of relevant information from the Internet , A lot of people say use Anchor and Dock attribute , But I tried ...

  9. WinForm in DataGridView Copy the selected cell content solution

    WinForm in DataGridView Select the cell content replication scheme with the mouse 1.CTR+C Shortcut key copy Premise : This control ClipboardCopyMode Property setting value is not Disable: 2. Mouse box selection , Custom code to achieve complex ...

Random recommendation

  1. reduce() function

    reduce() function reduce() The function is also Python A built-in higher-order function .reduce() Function to receive parameters and map() similar , A function f, One list, But behavior and map() Different ,reduce() Pass on ...

  2. 51nod 1117 A clever carpenter ( Huffman tree )

    subject : Portal . The question : Chinese questions . Answer key : It's about building a Huffman tree , Knowledge in data structure . #include <iostream> #include <cstdio> #include & ...

  3. delphi Generate network card MAC Address

    Generate MAC Address widget : {*------------------------------------------------ Generate mac Address @author @version 2015.7.2 20 ...

  4. MySQL Database learning notes ( Ten )----JDBC Transaction processing 、 encapsulation JDBC Tool class

    [ Statement ] Welcome to reprint , But please keep the original source of the article →_→ Life one :http://www.cnblogs.com/smyhvae/ Source of the article :http://www.cnblogs.com/smyhvae/p/4 ...

  5. Kafka In the source Producer Record Definition

    1.ProducerRecord meaning :  Send to Kafka Broker Of key/value  It's worth it 2. Internal data structure : -- Topic ( name ) -- PartitionID (  Optional ) -- Ke ...

  6. Count Color POJ--2777

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32217   Accepted: 9681 Desc ...

  7. Orcale Corpus of sentences

    Original address :http://www.cnblogs.com/omygod/archive/2007/08/31/876620.html Oracle Corpus of sentences 1. Oracle Initial password after installation ?  ...

  8. Vue2.x The parent-child components in communicate with each other

    Business scenario :( This refers to direct parent-child communication ) beauty ( Child components ) Send a message to a large group ( Parent component ) A large group ( Parent component ) After receiving the message from the beauty, send a message back to the beauty ( Child components ) Parent component template <template ...

  9. HTML5 Of article and section The difference between

    A content block is a block that puts HTML The unit after the page is logically divided . For page sites , Navigation menu . The body of the article . Every part of an article, such as comments, can be called a content block .      article Elements       article The element represents the document . The page or ...

  10. Java EE