Usage of check boxes in WinForm DataGridView control

Time:2021-9-16

System.Windows.Forms.DataGridViewIt is a data list container control provided in. Net forms, just like the rows and columns queried in MSSQL. It not only displays data, but also provides editing functions. Cells can even add unique control elements, such as text boxes, check boxes, and even pictures. This article introduces the check boxes.

You can add a for the DataGridView in the form designerColumnTypeValue isDataGridViewCheckBoxColumnThis is the check box columnTrueValueAndFalseValueProperty refers to the values of selected and unselected status respectively. These two values are used to judge the selected status later. Note that neither the check box nor the table container can be read-only, otherwise the mouse cannot click the check box.

To judge whether a row in the table is selected by the check box, just judge the value of the check box and get the line number. The code is as follows:

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
    {
        Console.WriteLine(i + " selected");
    }
}

The code assumes that there is a table named datagridview1, and the check box is the first column, that is, the first cell of each row. The continuity is to traverse the value of the first cell of each row.

Relevant environment:

.NET Framework 4.5

Recommended Today

Java Engineer Interview Questions

The content covers: Java, mybatis, zookeeper, Dubbo, elasticsearch, memcached, redis, mysql, spring, spring boot, springcloud, rabbitmq, Kafka, Linux, etcMybatis interview questions1. What is mybatis?1. Mybatis is a semi ORM (object relational mapping) framework. It encapsulates JDBC internally. During development, you only need to pay attention to the SQL statement itself, and you don’t need to […]