1

I fill a datagridview with an data table filled by an adapter. I have some columns that are smallint. They are used as flags, like booleans. How do I display this columns as checkboxes? Notice, that I can't change the database column type to boolean.

3 Answers 3

3

You just need to create an DataGridViewCheckBoxColumn then you tell it what's falseand what's true

this.ckbCol = new System.Windows.Forms.DataGridViewCheckBoxColumn();

this.dataGridView.Columns.Add(this.active);

this.ckbCol.DataPropertyName = "ACTIVE"; //if u want to bind it to a table or something
this.ckbCol.HeaderText = "Aktiv";
this.ckbCol.Name = "Aktiv";
//Now the important stuff follows!
this.ckbCol.FalseValue = "0";  
this.ckbCol.TrueValue = "1";

This works just fine for me and it's even possible to set it in the Designer!

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

Comments

0

You could use a TemplateColumn with DataBinder.Eval properly assigning the checked value to the checkbox or in the RowDataBound event handler you can check the row.DataItem and if your column is '1' you set the checkbox as checked. in the second case you get a reference to the checkbox control using (FindControl("checkboxId") as CheckBox)

Comments

0

You have 2 ways to do this if I remember my .Net correctly. First is the simple one don't use smallint use boolean and is will show you checkboxes by default. The second one is you have to do the gridview programatically. Use TemplateColumn and bind the data programatically in RowDataBound. her's a tutorial to help you get started http://www.asp.net/data-access/tutorials/adding-a-gridview-column-of-checkboxes-vb

Comments

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.