简体   繁体   English

在单选按钮中将bool转换为int

[英]Convert bool to int in a Radio Button

I have never used a Radio Button before. 我以前从未使用过单选按钮。 I have a datagridview and I added 3 radio buttons to my form to populate all the data, the is active data and not active data. 我有一个datagridview,并在表单中添加了3个单选按钮以填充所有数据,即活动数据而非活动数据。 This is what I am calling for the is active radio button, but it doesn't want to convert the bool to an int. 这就是我所需要的,是active单选按钮,但它不想将布尔值转换为int值。

private void rbIsActive_CheckedChanged(object sender, EventArgs e)
{
    string searchText = txtSearchText.Text;
    //bool sActive = true;
    //int i = sActive ? 1 : 0;
    bool IsActive = Convert.ToInt32(rbIsActive.Checked);
    var listBooks = GetBooks(searchText);
    grdBooks.DataSource = listBooks;
}

What am I doing wrong? 我究竟做错了什么?

不要转换为int,主要是因为您已将其分配给bool变量。

In this line: 在这一行:

bool IsActive = Convert.ToInt32(rbIsActive.Checked);

you convert a boolean to an int and are trying to save it into a bool variable. 您将布尔值转换为int并试图将其保存到bool变量中。 This obviously can not work. 这显然是行不通的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM