简体   繁体   中英

How to get a value of the checkbox in a GridView

I have made a C# windows forms project... How to get a value of the checkbox in a GridView - C# winforms

I add a gridview to the form...

the first column (with index 0) is checkbox button column...

I used the next code to access the checkbox value:

dataGridView1[0, 0].Value.ToString();

but it didn't work...

and when I try to execute:

MessageBox.Show(dataGridView1[0, 0].ValueType.ToString());

I got type.boolean...

so, How can I get the value of the checkbox...

Just tried...

Made a form with a datagridview with 1 column (checkbox) added a row then called

MessageBox.Show(dataGridView1[0, 0].Value.ToString());

shows "True" for me.

Probably just a casting issue.

like Hans says use

bool a = (bool)dataGridView1[0, 0].Value

您应该使用FormattedValue而不是Value

bool isChecked = (bool)dataGridView1[0, 0].FormattedValue;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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