如何在DataGridView中获取checkbox的值? [英] How to get the value of checkbox in the DataGridView?

查看:611
本文介绍了如何在DataGridView中获取checkbox的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在DataGridView中,有一个列,它的类型是"DataGridViewCheckBoxColumn",如何获取此复选框的状态?



ArrayList aList = new ArrayList();


foreach( dgv_Leaguer_Info .Rows)
{
bool bChecked = ??? ;
if(bChecked)
{
aList.Add(row.Cells [0] .Value。 ToString());
}
}



TKS!

解决方案

您需要使用名称或其索引(示例代码中的粗体)从行中获取单元格。为了避免错误,我将其转换为DataGridViewCheckBoxCell。将值转换为bool后,可以检查它是否为真(列已被选中)。


DataGridViewCheckBoxCell oCell;
foreach(DataGridViewRow行 dgv_Leaguer_Info .Rows)
{
oCell = row.Cells [" checkboxcolum的名称(或其索引) )< ]作为DataGridViewCheckBoxCell;
bool bChecked =(null!= oCell&& null!= oCell.Value&& true ==(bool)oCell.Value);
if(true == bChecked)
{
aList.Add(row.Cells [0] .Value.ToString());
}
}


In a DataGridView,There is a column,It's type is "DataGridViewCheckBoxColumn",How can I get the status of this checkbox?

 

ArrayList aList = new ArrayList();

foreach (DataGridViewRow row in dgv_Leaguer_Info.Rows)
            {
                bool bChecked = ???;
                if ( bChecked )
                {
                    aList.Add(row.Cells[0].Value.ToString());
                }
            }

 

TKS!

解决方案

You need to get the cell from the row using the name or its index (bold in the sample code). To avoid mistakes I cast it to DataGridViewCheckBoxCell. After casting the value to bool it can be checked if it is true (the column is checked) or not.

DataGridViewCheckBoxCell oCell;
foreach (DataGridViewRow row in dgv_Leaguer_Info.Rows)
{
   oCell = row.Cells["name of checkboxcolum (or its index)"] as DataGridViewCheckBoxCell;
   bool bChecked = (null != oCell && null != oCell.Value && true == (bool)oCell.Value);
   if (true == bChecked)
   {
      aList.Add(row.Cells[0].Value.ToString());
   }
}


这篇关于如何在DataGridView中获取checkbox的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆