ASP.NET 2.0 Gridview bit checkbox
-
09-06-2021 - |
Question
I have a column in my database that is a bit. The column gets into my gridview from a stored proc and its setting the column in the gridview to a checkbox, and thats ok. For all the other columns I am using
GridViewRow row = ((GridView)sender).SelectedRow;
textbox.Text = row.Cells[x].Text.ToString();
I have used this to set all the text boxes in my form from the database. But i am trying to find the code to access the checkbox in the gridview. I have tried
row.cells[bitColumn].Text.ToString
But that does not return a 1 or 0.
So my question is this when you have a checkbox in a gridview how do you access it? So that I could set a checkbox in my form based on this bit.
Solution
I don't like accessing columns by indexing them by number - you never know when they will change. Instead try to access them by name:
CheckBox cb = (CheckBox)Row.FindControl("chkSelector");
Where chkSelector
is an ASP.net checkbox control.
Then you can check if cb.Checked
. This will return a boolean as true / false you can then map that to your bit. If cb.Checked
is true
assign the value 1 else assign the value 0.