How to check if dataGridView checkBox is checked?

前端 未结 6 1767
猫巷女王i
猫巷女王i 2021-01-04 09:13

I\'m new to programming and C# language. I got stuck, please help. So I have written this code (c# Visual Studio 2012):

private void button2_Click(object sen         


        
6条回答
  •  礼貌的吻别
    2021-01-04 10:06

    All of the answers on here are prone to error,

    So to clear things up for people who stumble across this question,

    The best way to achieve what the OP wants is with the following code:

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        DataGridViewCheckBoxCell cell = row.Cells[0] as DataGridViewCheckBoxCell; 
    
        //We don't want a null exception!
        if (cell.Value != null)
        {
            if (cell.Value == cell.TrueValue)
            {
               //It's checked!
            }  
        }              
    }
    

提交回复
热议问题