0

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.

1
  • Because I am in a time crunch I went into my database and used a case statment and cast the result as a varchar forcing the gridview to bring in the column as text which i can access as i have been with the other columns. Though in the interest of reference. I will keep responding to this question to find the right way to do what i was trying to accomplish for those that may come after me. Commented May 15, 2012 at 16:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Does this work if i am autogenerating the columns. The checkbox column in the gridview is getting generated at runtime, so I am not sure how to access it.
Hi Michael, in this case just use your index if you would like. But assign it to a checkbox control Checkbox cb = (Checkbox)row.Cells[x]; and check if cb.Checked is true.
gotcha Thank you again for the quick repsonse and the information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.