Resolved Help for Converting

Ivo

Member
Joined
Feb 8, 2022
Messages
18
Programming Experience
Beginner
dgwWorkflow.CurrentRow.Cells[123] // is the checkedstate value in my SQL database example [ 1 ]

i tried this but thats not working : chLT_UTC1.Checked = dgwWorkflow.CurrentRow.Cells[123].Value.ToString() == "1";


how can i check myCheckbox on my winform
with the value from that cell ?? how to covert it to bool ? etc etc



Im novice
 
Last edited:
What does the cell actually contain? An int? An int?? A bool? A bool?? Something else? If you actually mean a SQL Server database (there's no such thing as a SQL database as SQL is a language that pretty much all databases use) then Boolean data should be stored in a bit column, which will map to bool values in C#.
 
i tried this but thats not working
What do you mean by it not working? What behavior are you seeing? Are you getting a compilation error or a runtime exception? Or are you always getting a false result? As noted above, what is the actual value?

As aside, you say that you are getting the value from a database, but it looks like you are using some kind of made up Hungarian notation and you are actually accessing either a DataGrid or DataGridView, not the database.

And as another aside, the .NET naming convention recommends not using Hungarian notation.
 
What does the cell actually contain? An int? An int?? A bool? A bool?? Something else? If you actually mean a SQL Server database (there's no such thing as a SQL database as SQL is a language that pretty much all databases use) then Boolean data should be stored in a bit column, which will map to bool values in C#.

It contains a BOOL a 0 or 1
I just dont know how to put that value of BOOL to a Checkbox in WinForm

Sorry im not clear, im realy beginner, and try to understand all....
Thanks in advance for all the help
 
It contains a BOOL a 0 or 1
No it doesn't. If it actually contains bool values then it contains true or false. The Checked property of a CheckBox is type bool so it's a direct assignment. The Value property of the cell is type object, so you need to cast:
C#:
chLT_UTC1.Checked = (bool)dgwWorkflow.CurrentRow.Cells[123].Value;
If it actually does contain 0 or 1 then it's a numeric type (byte, short, int or long), so you need to convert it to a bool. Rather than compare, you should actually convert:
C#:
chLT_UTC1.Checked = Convert.ToBoolean(dgwWorkflow.CurrentRow.Cells[123].Value);
That method will convert 0 to false and any other value to true.

As suggested, you really shouldn't be getting the data directly from the grid unless you're required to use an unbound grid. You should almost certainly be using a grid bound to a BindingSource and that to some data source, e.g. a DataTable. You then get the bound item from the BindingSource and the data from the item. We don't have enough information to go into specifics but you're probably using ADO.NET so you should probably use a data adapter to populate a DataTable and then bind that. Note that you can bind the data to other controls too, e.g. a CheckBox, so data is loaded automatically when the user selects a row in the grid. That would render this whole thread moot.
 
No it doesn't. If it actually contains bool values then it contains true or false. The Checked property of a CheckBox is type bool so it's a direct assignment. The Value property of the cell is type object, so you need to cast:
C#:
chLT_UTC1.Checked = (bool)dgwWorkflow.CurrentRow.Cells[123].Value;
If it actually does contain 0 or 1 then it's a numeric type (byte, short, int or long), so you need to convert it to a bool. Rather than compare, you should actually convert:
C#:
chLT_UTC1.Checked = Convert.ToBoolean(dgwWorkflow.CurrentRow.Cells[123].Value);
That method will convert 0 to false and any other value to true.

As suggested, you really shouldn't be getting the data directly from the grid unless you're required to use an unbound grid. You should almost certainly be using a grid bound to a BindingSource and that to some data source, e.g. a DataTable. You then get the bound item from the BindingSource and the data from the item. We don't have enough information to go into specifics but you're probably using ADO.NET so you should probably use a data adapter to populate a DataTable and then bind that. Note that you can bind the data to other controls too, e.g. a CheckBox, so data is loaded automatically when the user selects a row in the grid. That would render this whole thread moot.


Thanks for all, the chLT_UTC1.Checked = Convert.ToBoolean(dgwWorkflow.CurrentRow.Cells[123].Value); does work, and solved the problem
I learn a lot from the helping hands here on the forum, im Autodidact, so i try to learn from my mistakes, and the help from this nice people on this forum

Thanks again
Regards
Ivo
 
Back
Top Bottom