如何在DataGridView中获取checkbox的值? [英] How to get the value of checkbox in the DataGridView?
问题描述
在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屋!