使用wIndows Forms中的复选框重新设置数据网格列值 [英] Reterivng the data grid column value by using check box in wIndows Forms

查看:75
本文介绍了使用wIndows Forms中的复选框重新设置数据网格列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想通过复选框从数据网格视图中获取选中的列值。

但我在将字符串列值与bool进行比较时遇到了问题。

plesae找到下面用于显示已检查列值的代码。







foreach(DataGridViewRow dataGridView1.Rows中的行)

{



bool isselected = Convert.ToBoolean((row.Cells [clnname]。Value);(//将错误显示为字符串未被识别为有效布尔值 )

if(isselected == true)

{

message + = Environment.NewLine;

条消息+ = row.Cells [Name]。Value.ToString();



}

}

MessageBox.Show(选定值+消息);



请帮助我



谢谢

Hi,

I want to get the checked column value from the data gridview by using the checkbox.
But I am facing the problem while comparing the string column value to the bool.
plesae find the below code which i used for displaying the checked column value.



foreach (DataGridViewRow row in dataGridView1.Rows)
{

bool isselected = Convert.ToBoolean((row.Cells["clnname"].Value);(// Showing error as "string was not recognized as a valid boolean" )
if (isselected==true)
{
message += Environment.NewLine;
message += row.Cells["Name"].Value.ToString();

}
}
MessageBox.Show(" selected values" + message);

Please Help me

Thanks

推荐答案

如果它只是转换的问题,你应该使用bool.TryParse(row.Cells [clnname]。值,out isselected);



If its just an issue with the conversion, you should use bool.TryParse(row.Cells["clnname"].Value, out isselected);

bool isSelected = false;
bool.TryParse(row.Cells["clnname"].Value, out isSelected);





但是,如果你的问题是从DataGridViewCheckboxCell获取检查状态,那么必须要做几件事。



你需要设置TrueValue和列的FalseValue分别为true和false。



然后你需要检查TrueValue和FalseValue的复选框单元格值。如果它等于TrueValue,则检查它。





However, If your issue is with getting the checked state from a DataGridViewCheckboxCell, then there are a couple things that must be done.

You need to set the TrueValue and FalseValue of the column to true and false respectively.

Then You need to check the checkbox cell value against the TrueValue and FalseValue. If it equals the TrueValue, then it is checked.

foreach (DataGridViewRow row in dataGridView1.Rows) {
    bool isSelected = false;

    DataGridViewCheckBoxCell cell = row.Cells["clnname"] as DataGridViewCheckBoxCell;
    if (null != cell) {
         if (cell.Value == cell.TrueValue) {
              isSelected = true;
         }
    }

    if (isSelected) {
        message += Environment.NewLine;
        message += row.Cells["Name"].Value.ToString();
    }
}
MessageBox.Show(" selected values" + message);


这篇关于使用wIndows Forms中的复选框重新设置数据网格列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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